获取网页授权

在微信登录中,如何和获取网页授权。

一、登录微信测试公众品平台,修改网页授权基本信息,输入授权回调页面域名(自己的域名)。


然后重新建立一个tp框架 编写方法如图:

<?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;
    }
}

在公共模块中新建function.php

<?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_token;

猜你喜欢

转载自blog.csdn.net/Crimson1/article/details/80102399