How to achieve _php skills in PHP micro-credit development acquiring user information

Source: Internet
Author: User
Tags openid

PHP micro-Credit development Get user information

The approximate algorithm for obtaining user information is

Users are authorized to log on to third party websites,

Emphasis: Scope Parameters:
Snsapi_basic Silent Login, do not require user authorization, can only obtain OpenID;
Snsapi_userinfo, require user to click Authorization, can obtain OpenID and all user information;

The first step: Get the user's code value first;
The second step: according to the code value to get Access_token, each request value is different, if not used, every five minutes update;
The third step: according to Access_token to obtain user information;

1. Get Code implementation:

getcode.php

if (Isset ($_session[' user ')) {
              Print_r ($_session[' user '));
              Exit;
            }

$appid = ' wx1d7c6fcd6131143b3 ';

            $redirect _url= "http://www.antfortune.vip/callback.php";
            $scope = ' snsapi_userinfo '; the way of acquiring;


            $url = ' https://open.weixin.qq.com/connect/oauth2/authorize?appid= '. $appid. ' &redirect_uri= '. UrlEncode ($redirect _url). ' &response_type=code&scope= '. $scope. ' &state=123#wechat_redirect ';


Header ("Location:". $url);

2, according to the code to obtain Access_token and OpenID

getopenid.php



<?php
//Get user OpenID
$appid = "Your AppID";
$appsecret = "Your Appsecret";
$code =$_get[' Code '];



function Getopenid ($appid, $appsecret, $code) {
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid= ". $appid." &secret= ". 
$appsecret. " &code= ". $code." &grant_type=authorization_code ";

$weixin =file_get_contents ($url);//through code in exchange for Web page authorization Access_token
$jsondecode =json_decode ($weixin); Encode JSON-formatted strings
$array = Get_object_vars ($jsondecode);//convert array
$openid = $array [' OpenID '];//output OpenID return
$openid;
}

Echo Getopenid ($appid, $appsecret, $code);


Thank you for reading, I hope to help you, thank you for your support for this site!

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.