ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
``` ~~~ <?php ini_set('max_execution_time', '0'); class obj_openid ///定义openid对象 { public $openid=""; public $lang=""; } class obj_wxuser ///定义微信用户 { public $openid=""; public $nickname=""; } class weixin ///定义微信用户 { public $options = array( ); public function __construct($options) { $this->options = $options; } public function getToken(){ $access_token =getAccessToken($this->options); if(!$access_token){ return 80000; } return $access_token; } public function getUserList( $next_id = '' ){ //$wechat = new Wechat($this->options); $access_token = $this->getToken(); $extend = ''; if(!empty($next_id) ){ $extend = "&next_openid=$next_id"; } $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token={$access_token}$extend"; //echo $url; $ret = json_decode(curl_get( $url ),true); return $this-> getResult( $ret ); } public function getResult($ret){ if(is_array($ret)){ return array( 'total' => $ret['total'], 'list' => $ret['data']['openid'], 'next_id' => isset( $ret['next_openid'] ) ? $ret['next_openid'] : null ); }else{ return null; } } public function batGetUserinfo(){ $access_token = self::getToken(); $url = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token={$access_token}"; $list_openid=self::getUserList(); $count=count($list_openid["list"]); $i=0; $ret="{\"uils\":["; $arr_openid=array(); $arr_wxuser=array(); foreach ($list_openid["list"] as $value) { $i++; $obj=new obj_openid(); $obj->openid=$value; $obj->lang="zh-CN"; array_push($arr_openid,$obj); if($i%100==0) //因为微信官方只支持一次最多获取100个人的信息,故需多次提交 { $str_post="{\"user_list\":".json_encode($arr_openid)."}"; $ret= $ret.curl_get( $url, $str_post).","; $arr_openid=array(); } } if(count($arr_openid)>0) { $str_post="{\"user_list\":".json_encode($arr_openid)."}"; $ret =$ret.curl_get( $url, $str_post)."]}"; } else { $ret =$ret."]}"; } $ret =json_decode($ret, true ); return $this->getResult( $ret ) ? $ret : null; } } $options = array( 'appid'=>'XXXXXXX', //填写高级调用功能的app id 'appsecret'=>'XXXXXXXX', //填写高级调用功能的密钥 ); $weixinObj = new weixin($options); $ret = $weixinObj->batGetUserinfo(); //var_dump($ret); $count=count($ret["uils"]); $j=0; //echo $count; for($i=0;$i<$count;$i++) { foreach($ret["uils"][$i]["user_info_list"] as $val) { //我这里只获取了openid、nickname两项,如需获取更多请参考微信具体接口文档 $j++; // echo $j."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$val['openid']."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$val['nickname']."<br/>"; echo $val['openid']."<br/>"; } } function getAccessToken($options) { // 失效重新获取并更新数据库 $token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $options['appid'] . "&secret=" . $options['appsecret']; //$res = json_decode(file_get_contents($token_url, true), true); $res = json_decode(curl_get($token_url), true); // var_dump($res); $token = $res['access_token'] ?? ''; return $token; } // 发送post function send_post($url, $params='[]') { $curl = curl_init(); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POSTFIELDS, $params); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $data = curl_exec($curl); curl_close($curl); return json_decode($data, true); } function curl_get($url,$data=null){ $curl=curl_init(); curl_setopt($curl, CURLOPT_URL, $url); //设定为不验证证书和host curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if(!empty($data)){ curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } // 将curl_exec()获取的信息以文件流的形式返回,而不是直接输出 curl_setopt($curl,CURLOPT_RETURNTRANSFER,true); $output=curl_exec($curl); if (false===$output) { echo "",curl_error($curl),""; return false; } curl_close($curl); return $output; } ~~~ ```