A _php example of the method of obtaining user OpenID by micro-credit public platform

Source: Internet
Author: User
Tags openid

This article describes the micro-credit public platform to achieve access to the user OpenID method. Share to everyone for your reference. The specific analysis is as follows:

After the user clicks on the micro-Mail Custom menu view Type button, the micro-trust client will open the URL value that the developer fills in the button (that is, the link to the page) to open the Web page, but view does not get the user's OpenID, and needs to use the micro-letter "Web Licensing Access User profile" Advanced interface To obtain login personal information for the user.

Specific methods:

1, configure the Web page authorization callback domain name, such as Www.jb51.net

2, the simulation of the public number of Third-party Web pages, http://www.jb51.net/getcodeurl.php

<?php 
if (isset ($_session[' user ')) { 
 Print_r ($_session[' user '));
Exit;
}
$APPID = ' Public number in the APPID of micro-letters ';
$REDIRECT _uri= ' http://www.jb51.net/callback.php ';
$scope = ' snsapi_base ';
$scope = ' snsapi_userinfo ';/need authorization
$url = ' https://open.weixin.qq.com/connect/oauth2/authorize?appid= '. $APPID .' &redirect_uri= '. UrlEncode ($REDIRECT _uri). ' &response_type=code&scope= '. $scope. ' &state= '. $state. ' #wechat_redirect ';
Header ("Location:". $url);
? >

3, third-party Web page in the back-hop URL, first from the request to obtain code, and then according to code further exchange for OpenID and Access_token, and then can be based on OpenID and Access_token call the relevant interface of the micro-letter query user information.

<?php//http://www.jb51.net/callback.php $appid = "Public number in the AppID of micro-letters"; 
$secret = "Public number in the micro-letter app secret"; 
$code = $_get["code"]; $get _token_url = ' https://api.weixin.qq.com/sns/oauth2/access_token?appid= '. $appid. ' &secret= '. $secret. ' &code= '. $code. '
&grant_type=authorization_code ';
$ch = Curl_init (); 
curl_setopt ($ch, Curlopt_url, $get _token_url); 
curl_setopt ($ch, curlopt_header,0); 
curl_setopt ($ch, Curlopt_returntransfer, 1); 
curl_setopt ($ch, Curlopt_connecttimeout, 10); 
$res = curl_exec ($ch); 
Curl_close ($ch); 
$json _obj = Json_decode ($res, true); 
According to OpenID and access_token query user information $access _token = $json _obj[' Access_token ']; 
$openid = $json _obj[' OpenID ']; $get _user_info_url = ' https://api.weixin.qq.com/sns/userinfo?access_token= '. $access _token. ' &openid= '. $openid. ' 

&AMP;LANG=ZH_CN '; 
$ch = Curl_init (); 
curl_setopt ($ch, Curlopt_url, $get _user_info_url); 
curl_setopt ($ch, curlopt_header,0); 
curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_connecttimeout, 10); 
$res = curl_exec ($ch); 

Curl_close ($ch); 
Parse json $user _obj = Json_decode ($res, true); 
$_session[' user ' = $user _obj; 
Print_r ($user _obj); ?>

I hope this article will help you with the development of your PHP based micro-trust public platform.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.