1、openidusers.php
<?php
set_time_limit(600);
header("Content-type: text/html; charset=utf-8");
session_start();
$opts = array(
'http'=>array(
'method'=>"GET",
'timeout'=>1000,
)
);
$context = stream_context_create($opts);
$gettoken=file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx6c7xxxxxbddd4&secret=8ab524xxxcc4f6f4ac8617093", false, $context);
$res=json_decode($gettoken,true);
$token=$res["access_token"];
$_SESSION["token"]=$token;
$getopenid=file_get_contents("https://api.weixin.qq.com/cgi-bin/user/get?access_token={$token}&next_openid=", false, $context);
$res2=json_decode($getopenid,true);
$openid=$res2['data']['openid'];
foreach($openid as $k=>$v){
$getuser=file_get_contents("https://api.weixin.qq.com/cgi-bin/user/info?access_token={$token}&openid={$v}", false, $context);
$user=json_decode($getuser,true);
$userlist[]=$user;
}
$_SESSION["userllist"]=$userlist;
var_dump($userlist);
2、upd.php
<?php
header("Content-type: text/html; charset=utf-8");
session_start();
$link=mysql_connect("localhost","root","") or die('连接数据库失败!');
$db_selected = mysql_select_db('vshop', $link);
mysql_query("set names utf8;");
$query = 'select openid from wp_follow';
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$openidlist[]=$row['openid'];
}
$userlist=$_SESSION['userllist'];
$token = "gh_4df8efbb2caf";
foreach($userlist as $k=>$v){
if(in_array($v["openid"],$openidlist)){
@$v['nickname']=bin2hex($v['nickname']);
@$sql="update wp_follow set nickname='".$v['nickname']."',sex='".$v['sex']."',city='".$v['city']."',province='".$v['province']."',country='".$v['country']."',language='".$v['language']."',headimgurl='".$v['headimgurl']."',mTime='".time()."' where openid='".$v['openid']."'";
mysql_query($sql);
}else{
@$v['nickname']=bin2hex($v['nickname']);
@$sql2='insert into wp_follow(token,openid,nickname,sex,city,province,country,language,headimgurl,subscribe_time) values("'.$token.'","'.$v['openid'].'","'.$v['nickname'].'","'.$v['sex'].'","'.$v['city'].'","'.$v['province'].'","'.$v['country'].'","'.$v['language'].'","'.$v['headimgurl'].'","'.$v['subscribe_time'].'")';
mysql_query($sql2);
}
}
3、
<?php
header("Content-type: text/html; charset=utf-8");
$link=mysql_connect("127.0.0.1","root","") or die('连接数据库失败!');
$db_selected = mysql_select_db('vshop', $link);
mysql_query("set names utf8;");
$query = 'select * from wp_follow';
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$openidlist[]=hex2bin($row['nickname']);
}
var_dump($openidlist);
?>