以下为PHP接入微信平台开发 几个小例子,已经做了备注就不过多解释
public function getAllOpenid(){
//获取token,请替换下边的编号和密匙,公众号后台有,每次token的请求都会改变,这里token是身份验证的根据
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=编号&secret=密匙"; //根据微信提供接口获取
$ch = curl_init(); //初始化一个 cURL 对象
curl_setopt($ch, CURLOPT_URL, $url); //设置你需要抓取的URL
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //禁用后cURL将终止从服务端进行验证
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //可以设置为验证站点
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回原生的(Raw)输出
$output = curl_exec($ch); //抓取URL并把它传递给浏览器
curl_close($ch); //关闭cURL资源,并且释放系统资源
$jsoninfo = json_decode($output, true);
$access_token = $jsoninfo["access_token"];
echo "$access_token";
//获取单个用户信息
$url="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=oznizt77TsNMyl_6FQHVjZ_0pQBU&lang=zh_CN";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$jsoninfo = json_decode($output, true);
dump($jsoninfo);
//批量获取用户信息[post]
$url="https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=".$access_token;
$data[‘user_list‘][][‘openid‘]=‘ozniztzzpOFBVgdFxhkAUIIexvJ8‘;
$data[‘user_list‘][][‘openid‘]=‘oznizt9TbEMLzc4km2ETKSPH5oQQ‘;
$data=json_encode($data);
$output=$this->curlGet($url,‘post‘,$data);
$jsoninfo = json_decode($output, true);
dump($jsoninfo);
//获取用户列表
$url="https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$access_token."&next_openid=";
$output=$this->curlGet($url);
$jsoninfo = json_decode($output, true);
dump($jsoninfo);
}
//以下为curl方法,上边三个可以用这个方法。
function curlGet($url,$method=‘get‘,$data=‘‘){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, ‘Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)‘);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$temp = curl_exec($ch);
return $temp;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-03 21:23:57