//Get all user infors method with wechat PHP API
<?php
set_time_limit(0);
$count = 0;
//@description: convert the unicode to Chinese characters
//@para:target string
//@return:handled string
function decodeUnicode($str)
{
return preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
create_function(
'$matches',
'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'
),$str);
}
//@description: if the access_token is invaild, retrive the access_token and assign to global access_token
function isErr($data){
if(array_key_exists('errcode',$data)){
//the limit times to retrive access_token(for there is a upper limit 2000 one day)
if($GLOBALS['ERRCount'] > $GLOBALS['UpperLimit']){
return false;
}
if($data['errcode'] == 40001){
$GLOBALS['ERRCount'] = $GLOBALS['ERRCount'] + 1;
$GLOBALS['access_token'] = getaccess_token();
return true;
}
else{
//other error but not access_token invaild
return false;
}
}
return false;
}
//@description:get the access_token
function getaccess_token(){
//appid of wechat
$appid = '';
//appsecret of wechat
$appsecret = '';
//the access_token request url
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
$data = curl_exec($ch);
curl_close($ch);
//convert json to array
$data = json_decode($data,true);
return $data['access_token'];
}
//@description: post method of get 100 user infors(limit:100 once)
//@para:the first is access_token,the second is the post data which is a json string(format see wechat API website)
//@return: the array of user infors
function post($access_token, $post_data = ''){
$url = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token={$access_token}";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
// the response data which is the infor json string
$data = curl_exec($ch);
curl_close($ch);
// replace error json string to avoid decode error
$data = preg_replace('/[\x00-\x1F]/','', $data);
$data = json_decode($data,true);
//var_dump($data);
if(isErr($data)){
//if the access_token is invaild, retrive the new access_token and try again
return post($GLOBALS['access_token'],$post_data);
}
if(array_key_exists('user_info_list',$data)){
return $data['user_info_list'];
}
}
//@description: get the openids list of users(limit:10000 once)
//@para: the first is the access_token,the second is the next_openid which could be empty as the default
//@return: return the openid list
function getOpenid($access_token,$openid=''){
//获取access_token
$url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token={$access_token}&next_openid={$openid}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
$data = curl_exec($ch);
curl_close($ch);
$data = json_decode($data,true);
if(isErr($data)){
return getOpenid($GLOBALS['access_token'],$openid);
}
return $data;
}
//@description: write the user infor to file or do other handle
//para: user infor array
function handInfors($data){
if($data == null){
return;
}
foreach($data as $infor){
//number count of all users
$GLOBALS['InforCount'] = $GLOBALS['InforCount'] + 1;
//unicode convert
$inforJson = decodeUnicode(json_encode($infor));
//write each line(each user infor) to info.txt
$file = 'info.txt';
$current = file_get_contents($file);
$current .= $inforJson;
$current .= "\r\n";
file_put_contents($file, $current);
}
}
//@description: send model message to Specified openid
//@return: the send response
function set_msg($access_token,$openid){
$formwork = '{
"touser":"'.$openid.'",
"template_id":"GkChFMRCGYvGoNUzusXX3qnOOV-6U0L801L3lmp6NYo",
"url":"http://mp.weixin.qq.com/s/Nn7-KIVOR3S8SFBOV3Qw8Q",
"data":{
"first": {
"value":"缠论运行报告",
"color":"#173177"
},
"keyword1":{
"value":"缠论",
"color":"#173177"
},
"keyword2":{
"value":"国信TradeStation缠论全市场扫描",
"color":"#173177"
},
"keyword3":{
"value":"每天",
"color":"#173177"
},
"remark": {
"value":"2017-3-13:本日,\n第一类买点:2只,第二类买点:1只,第三类买点:0只;\n第一类卖点:1只,第二类卖点:0只,第三类卖点:2只;\n点击查看详情。",
"color":"#FF0000"
}
}
}';
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$access_token}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$formwork);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
//@description:get user infor by openid
//@return: the user infor Json string
function getUserByID($access_token,$openid){
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$access_token}&openid={$openid}&lang=zh_CN";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
$data = curl_exec($ch);
curl_close($ch);
return $data;//return the infor json string
}
//main method:
echo date('Y-m-d H:i:s',time());
echo("<br></br>");
$access_token = getaccess_token();
echo($access_token);
echo("<br></br>");
$ERRCount = 0;
$UpperLimit = 10;
$InforCount = 0;
$data = getOpenid($access_token);
while(true){
//getOpenid
$count = $data['count']; //openids count(10000 or trim)
$nextopenid = $data['next_openid'];
$list = array();
$tempCount = 0;
if(array_key_exists('data',$data) == false){
break;
}
foreach($data['data']['openid'] as $openid){
$tempCount = $tempCount + 1;
$idArray =array(
'openid' => $openid,
"lang" => "zh-CN"
);
Array_push($list,$idArray);
if(count($list) == 100 or $tempCount == $count){
$arra = array(
'user_list' => $list
);
echo(json_encode($arra));
$infors = post($access_token,json_encode($arra));
//write infors to files
handInfors($infors);
$list = array();
}
}
$data = getOpenid($access_token,$nextopenid);
}
echo date('Y-m-d H:i:s',time());
使用微信公众号接口一键获取全部关注用户的信息(openid,nickname,city,country,groupid,targetlist)
最新推荐文章于 2025-02-26 11:33:43 发布