How to obtain the openid and basic information of WeChat users through PHP

jacklove
Release: 2023-03-30 18:32:02
Original
8231 people have browsed it

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);}
Copy after login

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);    //打印用户信息 }
Copy after login

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);}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!