官方文档:https://kf.qq.com/faq/1901177NrqMr190117nqYJze.html
public function get_weixin_openid(){//用新的appid获取$token = model('Weixin')->get_access_token();$url = 'https://api.weixin.qq.com/cgi-bin/changeopenid?access_token='.$token;//获取老的openid//如果转变过了就is_change改成1$userArr = $this->db->table('user')->where('old_openid != '' AND is_change = 0')->order('`uid` desc')->limit(50)->lists();//没有了内容if(empty($userArr)){echo "over";die;}$openidArr = [];foreach($userArr as $item){$openidArr[] = $item['old_openid'];}$param['from_appid'] = '';//原来微信appid$param['openid_list'] = $openidArr;//原app下的openid$return = $this->http_post_data($url,json_encode($param));$openidNewArr = json_decode($return, TRUE);//获取转换的 保存起来即可foreach($openidNewArr['result_list'] as $openid){if(!empty($openid['err_msg'])&&$openid['err_msg']=='ok'){if(!empty($openid['new_openid'])){$this->db->table('User')->where("old_openid='".$openid['ori_openid']."'")->update(['openid'=>$openid['new_openid']]);}echo $openid['ori_openid'].'修改为'.$openid['new_openid'].'<br>';}$this->db->table('User')->where("old_openid='".$openid['ori_openid']."'")->update(['is_change'=>1]);}echo '<script>location.href="/index.php/weixin/get_weixin_openid"</script>';}function http_post_data($url, $data_string) {$ch = curl_init();curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8','Content-Length: ' . strlen($data_string)));ob_start();curl_exec($ch);$return_content = ob_get_contents();ob_end_clean();$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);return $return_content;}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号