5.1.3 实现代码

获取Access Token的实例代码如下。

1 <? php
2 $appid = "wxbad0b4x543aa0b5e";
3 $appsecret = "ed222a84da15cd24c4bdfa5d9ad1234";
4 $url = "https://api.weixin.qq.com/cgi-bin/token? grant_type=client_credential&appid=$appid&secret=$appsecret";
5
6 $ch = curl_init();
7 curl_setopt($ch, CURLOPT_URL, $url);
8 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
9 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
10 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
11 $output = curl_exec($ch);
12 curl_close($ch);
13 $info = json_decode($output, true);
14 var_dump($info);
15 ? >

上述代码执行以后,返回结果如下。

{
    "access_token":"NU7Kr6v9L9TQaqm5NE3OTPctTZx797Wxw4Snd2WL2HHBqLCiXlDVOw2l-Se0I-WmOLLniAYLAwzhbYhXNjbLc_KAA092cxkmpj5FpuqNO0IL7bB0Exz5s5qC9Umypy-rz2y441W9qg fnmNtIZWSjSQ",
    "expires_in":7200
}

这样就获得了Access Token。