4

配合这三篇实战操作
【微信服务号开发】01.接入指南
https://segmentfault.com/a/11...
【微信服务号开发】02.文字消息和图文消息自动回复实现
https://segmentfault.com/a/11...
【微信服务号开发】03.实战 回复知乎日报,返回8个最新知乎日报图文消息
https://segmentfault.com/a/11...

微信开发者工具

https://mp.weixin.qq.com/debu...

开发文档

https://mp.weixin.qq.com/wiki...

添加授权回调页面的域名


代码实现

把以下链接里面的参数替换掉

https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect 若提示“该链接无法访

替换以下参数:
appid
redirect_uri
scope

替换的代码

<?php
$appid = 'wx43953f2495f6d1cc';
$redirect_uri = urlencode('http://starks.ngrok.wdevelop.cn/act.php');
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$back}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect ";

header('Location:{$url}');

确认登录后

页面会访问这个url

http://starks.ngrok.wdevelop.cn/act.php?code=0615P8s314BY1P1CVSq31r98s315P8sI&state=STATE

通过code换取网页授权access_token

代码实现

require_once('Curl.php');
$code = $_GET['code'];
$appid = "****************************";
$secret = "****************************";
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$secret}&code={$code}&grant_type=authorization_code";
echo "<pre>";
$data = Curl::CurlGet($url);
$data = (array)json_decode($data);
var_dump($data);

返回数据

string(339) "{"access_token":"4_rBAOjGbsn_mnhXIP-5vu6dFk98tNG76faFqgvUSjwivpY8eSnvRlfXBSKilHA3vFNSXHA8kB9k0CWfHSYDid3g","expires_in":7200,"refresh_token":"4_PdBbZZl13PuA1w2sKIDAz4WhZcbozeYNcJfFDHBxfpsofo8b_1UfgS6H_cVqe1wQwfEEhmqTnQ_tzFScjTd3xg","openid":"oPs5ouLW3qg7P6CLj-jS7M1XVtSw","scope":"snsapi_userinfo","unionid":"o28P7ww-ZMphcik-5ZSbkCr_QTQw"}"

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

代码实现

$access_token = $data['access_token'];
$openid = $data['openid'];
$userurl = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN";
$userdata = Curl::CurlGet($userurl);
$userdata = (array)json_decode($userdata);
var_dump($userdata);

返回数据

string(343) "{"openid":"oPs5ouLW3qg7P6CLj-jS7M1XVtSw","nickname":"stark.wang","sex":1,"language":"zh_CN","city":"","province":"北京","country":"中国","headimgurl":"http:\/\/wx.qlogo.cn\/mmopen\/vi_32\/Q0j4TwGTfTJFHGcYsxe4TjLibiaDEicB8l2vv869kNLvfQ4P8WbO6adBmdTAH1s4h0JaH1oEZhU5icBApC6pXclF4Q\/0","privilege":[],"unionid":"o28P7ww-ZMphcik-5ZSbkCr_QTQw"}"

转换成数组格式

array(10) {
  ["openid"]=>
  string(28) "****************************"
  ["nickname"]=>
  string(10) "stark.wang"
  ["sex"]=>
  int(1)
  ["language"]=>
  string(5) "zh_CN"
  ["city"]=>
  string(0) ""
  ["province"]=>
  string(6) "北京"
  ["country"]=>
  string(6) "中国"
  ["headimgurl"]=>
  string(124) "http://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJFHGcYsxe4TjLibiaDEicB8l2vv869kNLvfQ4P8WbO6adBmdTAH1s4h0JaH1oEZhU5icBApC6pXclF4Q/0"
  ["privilege"]=>
  array(0) {
  }
  ["unionid"]=>
  string(28) "o28P7ww-ZMphcik-5ZSbkCr_QTQw"
}

全部代码

Curl.php
<?php
class Curl
{
   /**
     * cur get
     * @param  [type] $url [description]
     * @return [type]      [description]
     */
    public static function CurlGet($url){
      $data = '';
      $ch = curl_init();
      $header = "Accept-Charset: utf-8";
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
      // curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
      curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
      curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $temp = curl_exec($ch);
      return $temp;
      }
}
act.php
<?php
require_once('Curl.php');
$code = $_GET['code'];
$appid = "****************************";
$secret = "****************************";
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$secret}&code={$code}&grant_type=authorization_code";
echo "<pre>";
$data = Curl::CurlGet($url);
$data = (array)json_decode($data);
var_dump($data);


$access_token = $data['access_token'];
$openid = $data['openid'];
$userurl = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN";
$userdata = Curl::CurlGet($userurl);
$userdata = (array)json_decode($userdata);
var_dump($userdata);

西树先森
7.1k 声望926 粉丝

从事开发多年,前端、后端(go、Python、php)、服务架构都有涉猎,经历过大公司、创业公司,擅长前端及公司技术选型。