微信开发文档php

景正文
2023-12-01

第一步:基本配置

设置url(为资源服务器,可以进行token验证和返回token的一个地址下)。

Token为返回的一个token

EncodingAESKey:随机生成。

第二步:Appid和Appsecret设置和生成。

获取AppId和AppSecret:成为开发者后,在“开发者中心中”页面,可获取AppId和AppSecret

两个参数。

 

第三步:编写文件

在url路径中的文件中返回一个echostr。即可

若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,成为开发者成功(不过在此过程中需要进行signature对请求进行校验);可不进行校验

此GET请求中有四个参数:

signature:微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。

timestamp:时间戳

nonce:随机数

echostr:随机字符串.

)将token、timestamp、nonce三个参数进行字典序排序

2)将三个参数字符串拼接成一个字符串进行sha1加密

3)开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。

微信接入指南——wx_sample.php,可以返回文本信息。

define("TOKEN", "gewechatbtp");

$wechatObj = new wechatCallbackapiTest();

$wechatObj->valid();

$wechatObj->responseMsg();

 

class wechatCallbackapiTest

{

    public function valid()

    {

        $echoStr = $_GET["echostr"];

 

        //valid signature , option

        if($this->checkSignature()){

           echo $echoStr;

           exit;

        }

    }

 

    public function responseMsg()

    {

       $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

 

        //extract post data

       if (!empty($postStr)){

             

                libxml_disable_entity_loader(true);

              $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);

                $fromUsername = $postObj->FromUserName;

                $toUsername = $postObj->ToUserName;

                $keyword = trim($postObj->Content);

                $time = time();

                $textTpl = "<xml>

                         <ToUserName><![CDATA[%s]]></ToUserName>

                         <FromUserName><![CDATA[%s]]></FromUserName>

                         <CreateTime>%s</CreateTime>

                         <MsgType><![CDATA[%s]]></MsgType>

                         <Content><![CDATA[%s]]></Content>

                         <FuncFlag>0</FuncFlag>

                         </xml>";            

              if(!empty( $keyword ))

                {

                   $msgType = "text";

                   $contentStr = "Welcome to wechat world!";

                   $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);

                   echo $resultStr;

                }else{

                   echo "Input something...";

                }

 

        }else {

           echo "";

           exit;

        }

    }

      

    private function checkSignature()

    {

        // you must define TOKEN by yourself

        if (!defined("TOKEN")) {

            throw new Exception('TOKEN is not defined!');

        }

       

        $signature = $_GET["signature"];

        $timestamp = $_GET["timestamp"];

        $nonce = $_GET["nonce"];

              

       $token = TOKEN;

       $tmpArr = array($token, $timestamp, $nonce);

        // use SORT_STRING rule

       sort($tmpArr, SORT_STRING);

       $tmpStr = implode( $tmpArr );

       $tmpStr = sha1( $tmpStr );

      

       if( $tmpStr == $signature ){

           return true;

       }else{

           return false;

       }

    }

}

当已经验证通过后,就进行使用下列的

 

自定菜单:

直接运行改路径:http://gewechat.ge.com.cn/gehelp/testplan/menu1.php

 

 

网页授权的信息

用户在微信端访问第三方的网页,

在微信界面中的第三方网页,可以通过《微信的网页授权过后》就是给一个回调域名也就是第三方网页的域名,获取用户信息。

经过授权过后,就可以获取用户的信息

如果是用于进入该网页的openidscope的静态默认授权base

如果是用于获取该用户信息,则需要用户同意授权给公众号,为userinfo的授权。

 

         微信网页授权是通过OAuth2.0机制实现

         urlencode('http://gewechat.ge.com.cn/gehelp/tokenopen.php');

         http%3A%2F%2Fgewechat.ge.com.cn%2Fgehelp%2Ftokenopen.php\

         授权后的accesstoken     -----IUCLnCh0K7-SwE1BSObpRrFma-FNsoILt3JMOJ3UHGKmorMlMcpeafdldvU3M1Qa4V8WJqIZXrVHwLMGCd6DAuRzSVeAhScgxv3pmznyGa0

         用户授权给公众号,会获取到一个特有的网页授权的accesstoken(特有的接口调用凭证)

         https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxafed5daec5091a25&redirect_uri=.urlencode().&response_type=code&scope=snsapi_userinfo&state=wxbase#wechat_redirect

         redirect_uri?code=CODE&state=STATE可以获取code

         需要通过code来换取

                   https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxafed5daec5091a25&redirect_uri=http%3A%2F%2Fgewechat.ge.com.cn%2Fgehelp%2Ftokenopen.php&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect

                   redirect_uri?code=CODE&state=STATE可以获取code(GET里面,)

                  

                   https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxafed5daec5091a25&secret=571613c41cc46a56b4f89c9e26227a45&code=9118&grant_type=authorization_code

                   授权后的accesstoken     -----IUCLnCh0K7-SwE1BSObpRrFma-FNsoILt3JMOJ3UHGKmorMlMcpeafdldvU3M1Qa4V8WJqIZXrVHwLMGCd6DAuRzSVeAhScgxv3pmznyGa0

        

         网页授权的access_token可以进行授权后接口调用。

其他微信接口,需要通过基础支持中的“获取access_token”接口来获取到的普通access_token调用https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

检验授权凭证(access_token)是否有效

httpGET(请使用https协议) https://api.weixin.qq.com/sns/auth?access_token=ACCESS_TOKEN&openid=OPENID

{ "errcode":0,"errmsg":"ok"}

错误时的JSON返回示例:

{ "errcode":40003,"errmsg":"invalid openid"}

 

推送事件:

 

 

 

 

 

 

网页授权

 

直接通过获取codeGET请求,然后如果不为空,则获取授权token链接

"https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";

回调域名需要配置为gewechat.ge.com.cn

如果为空

$callback=urlencode("http://gewechat.ge.com.cn/gehelp/testplan/person_info.php");

回调的域名的里面含有code

$codeurl="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$callback."&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect";

 

Header(“Location: ”. $codeurl);

 

 

用户信息展示

<?php

header("Content-type:text/html;charset=utf-8");

 

 

$token_message= new Tokeninfomation();

$token =$token_message->gettoken();

$result=$token_message->getall_openid($token);

class Tokeninfomation{

    public function gettoken(){

        $appid="wx8350bd9db3b05ef3";

        $appsecret="50c1ade81b8bc5b5bbcb15af33af4a64";

        // "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx8350bd9db3b05ef3&secret=50c1ade81b8bc5b5bbcb15af33af4a64";

        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;

        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        $output = curl_exec($curl);

        curl_close($curl);

        $result = json_decode($output, true);

 

        $access_token = $result["access_token"];

        return $access_token;

 

    }

    public function getall_openid($token){

        $url ="https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$token."&next_openid=";

        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        $output = curl_exec($curl);

        curl_close($curl);

        $result = json_decode($output, true);

 

        $data= $result["data"];

        return $data['openid'];

    }

    public function getone_info($token,$openid){

        $url ="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$token."&openid=".$openid."&lang=zh_CN";

        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        $output = curl_exec($curl);

        curl_close($curl);

        $result = json_decode($output, true);

 

        return $result;

    }

}

?>

<html>

    <head>

        <title>通讯录</title>

    </head>

   

    <body style="background:#B7B7B7;">

    <div style="width:90%;margin:auto 5%">

      

       <div style="margin:10px;background-color:#5F9EA0;border-radius:10px;border-bottom: #cecdcd 2px dashed;">

           <div align="left" style="width:90%;height:10%">

               <p>&nbsp&nbsp&nbsp<input type="text" style="width: 95%;height:90%;border-radius:10px;" id="search" ></p>

           </div>

       </div>

       <?php

            for($i=0;$i<count($result);$i++){

                $oneinfo=$token_message->getone_info($token, $result[$i]);

                echo "<br>";

           

            ?>

 

       <a href="#"><div align="left" style="margin:10px;background-color:#4682B4;overflow:hidden;border-radius:10px;">

           <div style="width:25%;height:200px;float:left">

              <img src="<?php echo $oneinfo['headimgurl']?>" style="width: 60%;height:70%;margin:15% 10%;border-radius:5px;">

           </div>

               <input type="hidden" id="openid" value="<?php echo $oneinfo['openid']?>">

           <div style="margin:auto 1%;width:73%;height:200px;float:right;border-radius:10px; line-height:200px;">

              <span id="username" ><font size=6><?php echo $oneinfo['nickname']?></font></span>

           </div>

         </div></a>

            <?php

            }

            ?>

       <div align="center" style="margin:10px;height:100px;background-color:#698B22;overflow:hidden;border-radius:10px;line-height:100px;">

             <font size=7 >已经是最底了!</font>

       </div>

    </div>

</body>

</html>

 

 

 

 

 

 类似资料: