微信公众号的坑是真的多。算了,直接贴代码吧。
首先我们需要一个发送请求的自定义函数。
function http_post_daofeng($login_url, $data_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $login_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: multipart/form-data",
"Content-Length: " . strlen($data_string))
);
ob_start();
curl_exec($ch);
$return_content = ob_get_contents();
ob_end_clean();
$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return array($return_code, $return_content);
}
再加一个获取ACCESS_TOKEN的。
function token()
{
$file = 'token.txt';//用来存放token.我这里就放到txt文件里了,方便大家测试。
$mtime=filemtime( 'token.txt');
$time=time();
$time=$time-600;//设置一个时间吧,免得老是要去拿。
if($time<$mtime and file_get_contents($file)){
$token= file_get_contents($file);
}else{
$tokentxt = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=这里写你的APPID&secret=这里写APPsecret');//这个地方用全局吧!
$tokenarr = json_decode($tokentxt,true);
$token=$tokenarr[access_token];
$fp = fopen($file,"w");
fwrite($fp,$token);
fclose($fp);
}
return $token;
}
ok,这两个都有了,就去开始发送请求吧。
用这个代码。
$url = "https://api.weixin.qq.com/cgi-bin/guide/getguideacctlist?access_token=".token();//开始抓取身份信息
$data_string='{
"page": 0,
"num": 100
}';
list($return_code, $return_content) = http_post_daofeng($url, $data_string);//return_code是http状态码
echo $return_content;//这里我就直接输出看结果了
要注意的就是请求的数据包里不能带access_token参数,页码要从0开始。
下面是完整的代码。改一下你的appid和secret就可以用了,快去测试一下吧!
<?php
$url = "https://api.weixin.qq.com/cgi-bin/guide/getguideacctlist?access_token=".token();//开始抓取服务号顾问列表
$data_string='{
"page": 0,
"num": 100
}';
list($return_code, $return_content) = http_post_daofeng($url, $data_string);//return_code是http状态码
echo $return_content;//这里我就直接输出看结果了
function http_post_daofeng($login_url, $data_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $login_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: multipart/form-data",
"Content-Length: " . strlen($data_string))
);
ob_start();
curl_exec($ch);
$return_content = ob_get_contents();
ob_end_clean();
$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return array($return_code, $return_content);
}
function token()
{
$file = 'token.txt';//用来存放token.我这里就放到txt文件里了,方便大家测试。
$mtime=filemtime( 'token.txt');
$time=time();
$time=$time-600;//设置一个时间吧,免得老是要去拿。
if($time<$mtime and file_get_contents($file)){
$token= file_get_contents($file);
}else{
$tokentxt = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=这里写你的APPID&secret=这里写APPsecret');//这个地方用全局吧!
$tokenarr = json_decode($tokentxt,true);
$token=$tokenarr[access_token];
$fp = fopen($file,"w");
fwrite($fp,$token);
fclose($fp);
}
return $token;
}
?>