微信开发之开发者模式的启用

2018-05-19  本文已影响108人  liamu

启动配置

基本配置

对接代码

<?php
define("Token", "****");
define("appid","*****");
define("appsecret","*****");
     function getcurl($url)
     {
        $bbburl=$url;       
        $ch = curl_init();  
        curl_setopt($ch, CURLOPT_HEADER, 0);     
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
        curl_setopt($ch, CURLOPT_TIMEOUT, 5000);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//ssl 不验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);//ssl 不验证
        curl_setopt($ch, CURLOPT_URL,$bbburl ); 
        $res = curl_exec($ch); 
        return $res;
     }  
    function valide()
    {
        $signature = $_GET["signature"]; //微信的参数
        $timestamp = $_GET["timestamp"];//微信的参数
        $nonce = $_GET["nonce"];//微信的参数             
         
        $tmpArr = array(Token, $timestamp, $nonce);
        // use SORT_STRING rule
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if($tmpStr==$signature)
        {
             return true;
        }
        return false;    
    }
    function get_access_token()
    {
        return getcurl("https://api.weixin.qq.com/cgi-bin/token?"
        ."grant_type=client_credential&appid=".appid."&secret=".appsecret);
    }
    function sendText($toUser,$msg)
    {
        $str="<xml>"
        ."<ToUserName><![CDATA[$toUser]]></ToUserName>"
        ."<FromUserName><![CDATA[公众号原始ID]]></FromUserName>"
        ."<CreateTime>".time()."</CreateTime>"
        ."<MsgType><![CDATA[text]]></MsgType>"
        ."<Content><![CDATA[$msg]]></Content>"
        ."</xml>";
        return $str;
    }
    function sendNews($toUser)
    {
        $str="<xml>"
        ."<ToUserName><![CDATA[$toUser]]></ToUserName>"
        ."<FromUserName><![CDATA[公众号原始ID]]></FromUserName>"
        ."<CreateTime>".time()."</CreateTime>"
        ."<MsgType><![CDATA[news]]></MsgType>"
        ."<ArticleCount>1</ArticleCount>"
        ."<Articles>"
        ."<item>"
        ."<Title><![CDATA[今天天气很不错]]></Title> "
        ."<Description><![CDATA[夏天来了。要多喝水]]></Description>"
        ."<PicUrl><![CDATA[http://s4.sinaimg.cn/small/001J6wWgzy6THxCP2CL53&690]]></PicUrl>"
        ."<Url><![CDATA[https://www.jianshu.com/u/0cf04b676801]]></Url>"
        ."</item>"
        ."</Articles>"
        ."</xml>";
        return $str;
    }

    $getWeixin_msg = file_get_contents("php://input");
    if(valide() && !empty($getWeixin_msg))
    {
        libxml_disable_entity_loader(true);
        $postObj = simplexml_load_string($getWeixin_msg, 'SimpleXMLElement', LIBXML_NOCDATA);
        
        echo sendNews($postObj->FromUserName);
        // echo sendText($postObj->FromUserName,'hello world');
        exit();
    }

自定义菜单的实现

class  Wechat   
{      
    public $APPID="*************";      
    public $APPSECRET="******************";  
    //获取access_token  
    public function index()  
    {         
        $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->APPID."&secret=".$this->APPSECRET;       
        $date = postcurl($url); 
        $access_token = $date['access_token']; 
        return $access_token;         
    }  
    //拼接参数,带着access_token请求创建菜单的接口  
    public function createmenu(){  
        $data='{  
            "button":[  
                {      
                    "type":"view",  
                    "name":"听歌",  
                    "url":"http://music.163.com/"  
                },
                {  
                    "name":"我的",  
                    "sub_button":[  
                        {      
                            "type":"click",  
                            "name":"联系我们",  
                            "key":"CONTACTUS"  
                        },  
                        {  
                            "type":"view",  
                            "name":"历史文章",  
                            "url":"https://www.jianshu.com/u/0cf04b676801"  
                        }
                    ]  
                }        
            ]  
        }';      
        $access_token = $this->index();  
        $url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;    
        $result=postcurl($url,$data);  
        var_dump($result);             
    }
}
$obj = new Wechat();
$obj->createmenu();

直接访问这个类就可以啦,会自动将数据发送到微信服务器端

上一篇下一篇

猜你喜欢

热点阅读