Get web page authorization

In WeChat login, how and how to obtain webpage authorization.

1. Log in to the WeChat test public product platform, modify the basic information of web page authorization, and enter the domain name of the authorization callback page (your own domain name).


Then re-establish a tp framework to write the method as shown in the figure:

<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
       $appid='wx27f664ab15ecb71d';
       $redirect_uri=urlencode('http://www.crimson1.top/vote/index.php/home/index/getcode');
       $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";

             header("Location:".$url);
    }

    public function getcode(){
    	$code=$_GET["code"];
    	 $json=$this->access_token($code);
    	 echo $json;
    }
    public function access_token($code){
        $appid="wx27f664ab15ecb71d";
        $appsecret="015756334f2982ed1189c6d66dbc0353";
        $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";

        $ret=https_request($url);
        return $ ret;
    }
}

Create a new function.php in the public module

<?php
function https_request($url){
	$curl=curl_init();
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	$data=curl_exec($curl);
	if(curl_errno($curl)){
		return 'ERROR'.curl_error($curl);
	}
	curl_close($curl);
	return $data;
}

Access on your own mobile phone, you can get access_token;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325226070&siteId=291194637
Recommended