再看WX公众号开发文档,看到部分接口的请求方式为GET,让我想到PHP中有一个函数file_get_contents用来获取文档内容为一个字符串,那这个函数貌似可以替代自己写的curl函数,这就写个小案例实现一下判断用户是否关注公众号。
<?php
//获取access_token
$token = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET');
$actoken = json_decode($token);
$openid = '替换为openid';
//查询是否关注
$subStr = sprintf('https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN',$actoken->access_token,$openid);
$result = json_decode(file_get_contents($subStr));
var_dump($result);
// 1为已关注,0未关注/取消关注
?>
这个方式是不是简单粗暴?反正小项目我就直接用了