Home > Mobile >  Newbie questions: web authorization for user information, unable to get the right feedback
Newbie questions: web authorization for user information, unable to get the right feedback

Time:09-25

Newbie questions, in accordance with the page WeChat interface can get the correct information, but written in PHP code to get the correct information
Also, please have a great god can help guide
Me is the information of reference books, is weixin. Class. PHP code, APPID and appsecret did not express
 class class_weixin 
{
Var $appid=appid;
Var $appsecret=appsecret;

//constructor, to obtain the Access Token
The public function __construct ($appid=NULL, $appsecret=NULL)
{
If ($appid & amp; & {$appsecret)
$this - & gt; Appid=$appid;
$this - & gt; Appsecret=$appsecret;
}
//2. The cache form
If (isset ($_SERVER [' HTTP_APPNAME '])) {//SAE environment, need to open the memcache
$mem=memcache_init ();
} else {//local environment, need to be installed memcache
$mem=new Memcache;
$mem - & gt; The connect (' localhost ', 11211) or die (" Could not connect ");
}
$this - & gt; Access_token=$mem - & gt; Get ($this - & gt; Appid);
if (! Isset ($this - & gt; Access_token) | | the empty ($this - & gt; Access_token)) {
$url="https://api.weixin.qq.com/cgi-bin/token? Grant_type=client_credential & amp; Appid=". $this - & gt; Appid. "& amp; Secret=". $this - & gt; Appsecret;
$res=$this - & gt; Http_request ($url);
$result=json_decode ($res, true);
$this - & gt; Access_token=$result (" access_token ");
$mem - & gt; The set ($this - & gt; Appid, $this - & gt; Access_token, 0, 3600);
}

}

//generated OAuth2 URL
The public function oauth2_authorize ($redirect_url, $scope, $state=NULL)
{
$url="https://open.weixin.qq.com/connect/oauth2/authorize? Appid=". $this - & gt; Appid. "& amp; Redirect_uri=". $redirect_url. "& amp; The payload=code& The scope=". $scope. "& amp; The state=". $state. "# wechat_redirect";
Return $url;
}

//generated OAuth2 the Access Token
The public function oauth2_access_token ($code)
{
$url="https://api.weixin.qq.com/sns/oauth2/access_token? Appid=". $this - & gt; Appid. "& amp; Secret=". $this - & gt; Appsecret. "& amp; Code=". $code. "& amp; Grant_type=authorization_code ";
$res=$this - & gt; Http_request ($url);
Return json_decode ($res, true);
}

//get the user basic information (OAuth2 authorization Access Token Access did not focus on the user, the Access Token for temporary Access)
The public function oauth2_get_user_info ($access_token, $openid)
{
$url="https://api.weixin.qq.com/sns/userinfo? Access_token=". $access_token. "& amp; Openid=". $openid. "& amp; Lang=zh_CN ";
$res=$this - & gt; Http_request ($url);
Return json_decode ($res, true);
}

//get the user basic information
The public function get_user_info ($openid)
{
$url="https://api.weixin.qq.com/cgi-bin/user/info? Access_token=". $this - & gt; Access_token. "& amp; Openid=". $openid. "& amp; Lang=zh_CN ";
$res=$this - & gt; Http_request ($url);
Return json_decode ($res, true);
}

//HTTP requests (HTTP/HTTPS support, support the GET/POST)
Protected function http_request ($url, $data=https://bbs.csdn.net/topics/null)
{
$curl=curl_init();
Curl_setopt ($curl, CURLOPT_URL, $url);
Curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
Curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (! Empty ($data)) {
curl_setopt($curl, CURLOPT_POST, 1);
Curl_setopt ($curl, CURLOPT_POSTFIELDS, $data);
}
Curl_setopt ($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output=the curl_exec ($curl);
Curl_close ($curl);
Return $output;
}
}

Below is the index of PHP code page
 & lt; ? PHP 

Echo "hello 111";
Require_once (' weixin. Class. PHP);
$weixin=new class_weixin ();

if (! Isset ($_GET [] "code")) {
$redirect_url='http://'. $_SERVER [' HTTP_HOST] $_SERVER [' REQUEST_URI '];
$jumpurl=$weixin - & gt; Oauth2_authorize ($redirect_url, "snsapi_base", "123");
The Header (" Location: $jumpurl ");
Echo "hello yes";
} else {
Echo "hello err1";
$access_token_oauth2=$weixin - & gt; Oauth2_access_token ($_GET [] "code");
The userinfo=$weixin - & gt; Oauth2_get_user_info ($access_token_oauth2 [' access_token '], $access_token_oauth2 [' openid ']);
Echo "hello err";
}
Echo "hello end2";
?>

Again is the HTML code below, in the index. PHP is no shows, the echo such as "hello" above is in order to confirm whether there is read into the index set by the
But there has been no way to get the correct value
  • Related