博客列表 >微信openid转换工具,使用旧的openid转换到新的openid

微信openid转换工具,使用旧的openid转换到新的openid

大瓶可乐
大瓶可乐原创
2020年12月15日 13:33:361782浏览

官方文档:https://kf.qq.com/faq/1901177NrqMr190117nqYJze.html

  1. public function get_weixin_openid()
  2. {
  3. //用新的appid获取
  4. $token = model('Weixin')->get_access_token();
  5. $url = 'https://api.weixin.qq.com/cgi-bin/changeopenid?access_token='.$token;
  6. //获取老的openid
  7. //如果转变过了就is_change改成1
  8. $userArr = $this->db->table('user')->where('old_openid != '' AND is_change = 0')->order('`uid` desc')->limit(50)->lists();
  9. //没有了内容
  10. if(empty($userArr))
  11. {
  12. echo "over";
  13. die;
  14. }
  15. $openidArr = [];
  16. foreach($userArr as $item)
  17. {
  18. $openidArr[] = $item['old_openid'];
  19. }
  20. $param['from_appid'] = '';//原来微信appid
  21. $param['openid_list'] = $openidArr;//原app下的openid
  22. $return = $this->http_post_data($url,json_encode($param));
  23. $openidNewArr = json_decode($return, TRUE);
  24. //获取转换的 保存起来即可
  25. foreach($openidNewArr['result_list'] as $openid)
  26. {
  27. if(!empty($openid['err_msg'])&&$openid['err_msg']=='ok')
  28. {
  29. if(!empty($openid['new_openid'])){
  30. $this->db->table('User')->where("old_openid='".$openid['ori_openid']."'")->update(['openid'=>$openid['new_openid']]);
  31. }
  32. echo $openid['ori_openid'].'修改为'.$openid['new_openid'].'<br>';
  33. }
  34. $this->db->table('User')
  35. ->where("old_openid='".$openid['ori_openid']."'")
  36. ->update(['is_change'=>1]);
  37. }
  38. echo '<script>location.href="/index.php/weixin/get_weixin_openid"</script>';
  39. }
  40. function http_post_data($url, $data_string) {
  41. $ch = curl_init();
  42. curl_setopt($ch, CURLOPT_POST, 1);
  43. curl_setopt($ch, CURLOPT_URL, $url);
  44. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  45. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  46. 'Content-Type: application/json; charset=utf-8',
  47. 'Content-Length: ' . strlen($data_string))
  48. );
  49. ob_start();
  50. curl_exec($ch);
  51. $return_content = ob_get_contents();
  52. ob_end_clean();
  53. $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  54. return $return_content;
  55. }
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议