Home >Backend Development >PHP Tutorial >How to obtain the openid and basic information of WeChat users through PHP
This article explains the operation of obtaining the openid and basic information of WeChat users through PHP.
##Basic configuration
public function getcode(){ //基本配置 $appid=''; $redirect_uri=urlencode("https://授权回调页面域名/plugs/task/getuserinfo"); $url=" header("location:".$url);}Get information
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 request
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);}
This article explains how to obtain the openid and basic information of WeChat users through PHP. For more related content, please pay attention to the PHP Chinese website.
Related recommendations:
php code to implement 12306 remaining ticket query and price query functions
Introducing tutorials related to PHP's quick export of Table data
Explain how to use ArrayAccess, the PHP predefined interface
The above is the detailed content of How to obtain the openid and basic information of WeChat users through PHP. For more information, please follow other related articles on the PHP Chinese website!