Created
August 19, 2017 14:39
-
-
Save qianlongzt/3fd2ff851c757b5025c11b3918d49bd7 to your computer and use it in GitHub Desktop.
微信 Oauth 回调域名和使用域名不一样的使用。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$appid = APPID; | |
$appkey = APPKEY; | |
$redirect_url = urlencode(REDIRECT_URL);//可以为跳板url,然后跳板url跳到这里来。域名可以不一样。 | |
$scope = "snsapi_userinfo";//snsapi_userinfo //这个可以不用关注; | |
$state = ""; | |
if(isset($_GET['code'])) { | |
$code = $_GET['code']; | |
$api_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$appkey}&code={$code}&grant_type=authorization_code"; | |
$data_str = get($api_url); | |
$data = json_decode($data_str, true); | |
var_dump($data); | |
} else { | |
$oauth_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$redirect_url}&response_type=code&scope={$scope}&state={$state}#wechat_redirect"; | |
header("location: {$oauth_url}"); | |
} | |
function get($url) { | |
$curl = curl_init($url); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_TIMEOUT, 2); | |
return curl_exec($curl); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$back_url = BACKURL; // oauth 所在的 URL | |
if(isset($_GET['code'])) { | |
$code = $_GET['code']; | |
$state = $_GET['state']; | |
$url = "{$back_url}?code={$code}&state={$state}"; | |
header("location: {$url}"); | |
} else { | |
header("HTTP/1.1 403 Forbidden"); | |
echo "403 Forbidden"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment