//1.请求url地址
$appid = 'wx710f6c90457f55da';
$appsecret = '335303cfe1b8b1cc13bdcbebf4bfce2b';
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
//2初始化
$ch = curl_init();
//3.设置参数
curl_setopt($ch , CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
//4.调用接口
$res = curl_exec($ch);
//5.关闭curl
curl_close( $ch );
if( curl_errno($ch) ){
var_dump( curl_error($ch) );
}
$arr = json_decode($res, true);
var_dump( $arr );
}
在这段的代码中,不能缺少 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
如果缺少的话,就不能能出现下面的结果:
array(2) { ["access_token"]=> string(138) "fz3V0gQJhGVXlQIoJoSWFYMyy1F602CR6zD4sTGovBiJufrCosjfghiATqPge2kUxRITGoJHbT4C3cc6egO46q20ujhcCxrIa0-fmAFs0IEyN9ndlhF9Lh9HwwcCwYFmJQEaAIAWDR" ["expires_in"]=> int(7200) }
结果是null,怎么弄都不会有这种结果的!
2:实现页面的成功调用--代码;
function http_curl(){
//获取imooc
//1.初始化curl
$ch = curl_init();
$url = 'http://www.baidu.com';
//2.设置curl的参数
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//3.采集
$output = curl_exec($ch);
//4.关闭
curl_close($ch);
var_dump($output);
}
3:
function getWxServerIp(){
$accessToken = "dTl5m7Fgz_bF7ShRI-1D4QiU_v17pnPIFRUIqSmzI6LA6dkuvMIOJ1aV9VeQmzbA7knq8mlpn8GNqg8W52gnUGY1dEUhFZapv4ZBM08gTDKR9s1I-xx4cO_EGya1k-ITMRJeAFAUZRzcVc235w_9zONy75NoO11gC9P0o4FBVxwvDiEtsdX6ZRFR0Lfs_ymkb8Bf6kRfo";
$url = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=".$accessToken;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
$res = curl_exec($ch);
curl_close($ch);
if(curl_errno($ch)){
var_dump(curl_error($ch));
}
$arr = json_decode($res,true);
echo "<pre>";
var_dump( $arr );
echo "</pre>";
}
返回结果为:
array(2) {
["errcode"]=>
int(40001)
["errmsg"]=>
string(81) "invalid credential, access_token is invalid or not latest hint: [_2KBNA0206vr29!]"
}