微信开发

来源:互联网 发布:矩阵理论与应用研究生 编辑:程序博客网 时间:2024/06/05 04:47

获取接口调用凭据–获取access_token

access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token
注意调用所有微信接口时均需使用https协议。
接口调用请求说明
http请求方式: GET
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

参数说明
这里写图片描述

返回说明

正常情况下,微信会返回下述JSON数据包给公众号:

{"access_token":"ACCESS_TOKEN","expires_in":7200}

这里写图片描述

public static function getAccessToken() {        // access_token 应该全局存储与更新,以下代码以写入到文件中做示例        $baseurl = dirname(dirname(dirname(self::$jsonUrl))).'/public/json/';        $file = $baseurl."access_token.json";        if(file_exists($file)){            $data = json_decode(file_get_contents($file));        }        if (!isset($data)||$data->expire_time < time()) {          // 如果是企业号用以下URL获取access_token          // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=".APPID."&corpsecret=".APPSECRET;          $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET;          $res = json_decode(self::httpGet($url));          $access_token = $res->access_token;          if(!isset($data)){            $data = new stdClass();          }          if ($access_token) {            $data->expire_time = time() + 7000;            $data->access_token = $access_token;            $fp = fopen($baseurl."access_token.json", "w");            fwrite($fp, json_encode($data));            fclose($fp);          }        } else {          $access_token = $data->access_token;        }        return $access_token;    }    public static function httpGet($url,$data='') {        $curl = curl_init();        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);        curl_setopt($curl, CURLOPT_TIMEOUT, 500);        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);        curl_setopt($curl, CURLOPT_URL, $url);        if($data){            curl_setopt ( $curl, CURLOPT_POST, 1 );            curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data );        }        $res = curl_exec($curl);        curl_close($curl);        return $res;    }

将access_token获取后存储到本地,设置有效期时间,到过效后重新获取

​获取用户基本信息(UnionID机制)

开发者可通过OpenID来获取用户基本信息。请使用https协议。

接口调用请求说明

http请求方式: GET
https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN

参数说明

这里写图片描述

返回说明

这里写图片描述

示例:

public static function getBaseUserInfo($openid){        $accessToken = self::getAccessToken();        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$accessToken."&openid=".$openid."&lang=zh_CN";        $res = json_decode(self::httpGet($url));        return $res;    }

网页授权获取用户基本信息

这里写图片描述

1)授权流程:

=》用户同意授权,获取code

https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect​

参数个数,顺序,错误则无法访问​,回调地址redirect_uri,应使用https链接来确保授权code的安全性

这里写图片描述

=》通过code换区网页授权access_token​

请求方法

获取code后,请求以下链接获取access_token:
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code

参数说明

这里写图片描述

public static function getBaseUserInfo($openid){        $accessToken = self::getAccessToken();        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$accessToken."&openid=".$openid."&lang=zh_CN";        $res = json_decode(self::httpGet($url));        return $res;    }

返回说明

这里写图片描述

=》拉取用户信息(需scope为snsapi_userinfo)

如果网页授权作用域为snsapi_userinfo,则此时开发者可以通过access_token和openid拉取用户信息了。

请求方法

http:GET(请使用https协议)
https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN

参数说明

这里写图片描述

返回说明

这里写图片描述

微信开发文档:

http://mp.weixin.qq.com/wiki/7/2d301d4b757dedc333b9a9854b457b47.html​

0 0
原创粉丝点击