1. 程式人生 > >微信登陸失敗redirect_uri 域名與後臺配置不一致 10003(thinkphp)

微信登陸失敗redirect_uri 域名與後臺配置不一致 10003(thinkphp)

public function index(){

$redirect_url = 'http://www.ceshi.com/ceshi';//設定域名時沒有http://,在獲取code時必須有,否則將一直顯示10003錯誤

$a = $this->baseAuth($redirect_url);

}

//設定網路請求配置

public function _request($curl,$https=true,$method='GET',$data=null){

// 建立一個新cURL資源

$ch = curl_init();

// 設定URL和相應的選項

curl_setopt($ch, CURLOPT_URL, $curl); //要訪問的網站

curl_setopt($ch, CURLOPT_HEADER, false); //啟用時會將標頭檔案的資訊作為資料流輸出。

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //將curl_exec()獲取的資訊以字串返回,而不是直接輸出。

if($https){

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //FALSE 禁止 cURL 驗證對等證書(peer's certificate)。

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); //驗證主機

}

if($method == 'POST'){

curl_setopt($ch, CURLOPT_POST, true); //傳送 POST 請求

curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //全部資料使用HTTP協議中的 "POST" 操作來發送。

}

// 抓取URL並把它傳遞給瀏覽器

$content = curl_exec($ch);

if ($content === false) {

return "網路請求出錯: " . curl_error($ch);

exit();

}

//關閉cURL資源,並且釋放系統資源

curl_close($ch);

return $content;

}

/**

* 獲取使用者的openid

* @param string $openid [description]

* @return [type] [description]

*/

public function baseAuth($redirect_url){

//1.準備scope為snsapi_base網頁授權頁面

$baseurl = urlencode($redirect_url);

// p($baseurl);

//公眾號

$appid = 'wx111111111111';//填寫自己的appid

$appsecret = '3213123dasdasdas3123131';//填寫自己的appsecret

$snsapi_base_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$baseurl.'&response_type=code&scope=snsapi_base&state=YQJ#wechat_redirect';

// p($snsapi_base_url);

//2.靜默授權,獲取code

//頁面跳轉至redirect_uri/?code=CODE&state=STATE

$code = $_GET['code'];

// p($code);die;

if( !isset($code) ){

header('Location:'.$snsapi_base_url);

}

// p($code);die;

//3.通過code換取網頁授權access_token和openid

$curl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';

$content = $this->_request($curl);

$result = json_decode($content,true);

// p($content);die;

return $result;

}