微信公众号获取服务号顾问列表php代码

微信公众号的坑是真的多。算了,直接贴代码吧。

首先我们需要一个发送请求的自定义函数。

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;
	}





?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱钓鱼的程序员老周

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值