Rumah  >  Artikel  >  pembangunan bahagian belakang  >  如何通过PHP获取微信用户的openid和基本信息

如何通过PHP获取微信用户的openid和基本信息

jacklove
jackloveasal
2018-06-08 13:46:578185semak imbas

本篇讲解通过PHP获取微信用户的openid和基本信息的操作。

基本配置

public function getcode(){    //基本配置    
$appid='';    $redirect_uri=urlencode("https://授权回调页面域名/plugs/task/getuserinfo");   
$url=" 
header("location:".$url);}

获取信息

public function getuserinfo(){    $appid  = "";    $secret = "";     //这里获取到了code  
$code   = $_GET['code'];     //第一步:取得openid   
$oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code";    
$oauth2 = $this->http_curl($oauth2Url);    //accestoken  
$access_token = $oauth2["access_token"];    //openid    $openid = $oauth2['openid'];//第二步:根据全局access_token和openid查询用户信息
$get_user_info_url = "https://api.weixin.qq.com/sns/userinfoaccess_token=".$access_token."&openid=".$openid."&lang=zh_CN";
$userinfo = $this->http_curl($get_user_info_url);     dump($userinfo);    //打印用户信息 }

curl请求

function http_curl($url){//用curl传参
$ch = curl_init();    
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//关闭ssl验证    
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);     
curl_setopt($ch,CURLOPT_HEADER, 0);   
$output = curl_exec($ch);
curl_close($ch);   
return json_decode($output, true);}

本文讲解了如何通过PHP获取微信用户的openid和基本信息,更多相关内容请关注php中文网。

相关推荐:

php代码实现12306余票查询、价格查询功能

介绍PHP快速导出Table数据相关教程

讲解PHP预定义接口之ArrayAccess的使用方法

Atas ialah kandungan terperinci 如何通过PHP获取微信用户的openid和基本信息. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:PHP读取配置文件类实例Artikel seterusnya:php实现新闻发布系统