WeChat public platform realizes the method of obtaining user OpenID _php

Source: Internet
Author: User
Keywords WeChat public platform access user OpenID
Tags openid
In this paper, we describe the method of public platform to obtain user OpenID. Share to everyone for your reference. The specific analysis is as follows:

After the user clicks on the Custom menu view Type button, the client will open the URL value (ie link) that the developer fills in the button to reach the Web page, but the view cannot get the user's OpenID, and it needs to use the advanced interface of "Web license Access user basic information", Get the user's login personal information.

Specific methods:

1, configure the page authorization callback domain name, such as Www.bitsCN.com

2. Third party Web page of The Sims, http://www.bitsCN.com/getcodeurl.php

<?php if (isset ($_session[' user ')) {  Print_r ($_session[' user '); exit;} $APPID = ' APPID of the public number '; $REDIRECT _uri= ' http://www.bitsCN.com/callback.php '; $scope = ' snsapi_base ';//$scope = ' Snsapi_ UserInfo ';//need to authorize $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, the Third party Web page bounce URL, first get code from the request, and then according to code further in exchange for OpenID and Access_token, and then can be based on the OpenID and Access_token invoke the relevant interface to query user information.

<?php//http://www.bitscn.com/callback.php$appid = "AppID of the public number"; $secret = "Public number in 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); Query user information based on OpenID and access_token $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);?>

It is hoped that this article will help you to develop PHP-based 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.