0
public function get_code()
{
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$client_id = 'my_client_id';
$redirect_url = HTTP_ROOT.'users/get_code';
$code = $_GET['code'];
//perform post request now
$opts = array(
'http' => array(
'method' => 'POST',
'header' => "Accept: application/json\r\nContent-type: application/x-www-form-urlencoded\r\n",
'user_agent' => $user_agent,
'content' => http_build_query(
array(
'client_id' => $client_id,
'client_secret' => 'my_secert_id',
'code' => $code
)
)
)
);
$context = stream_context_create($opts);
$json_data = file_get_contents("https://github.com/login/oauth/access_token", false, $context);
$r = json_decode($json_data , true);
$access_token = $r['access_token'];
$url = "https://api.github.com/user?access_token=". urlencode($access_token);
// $data = file_get_contents($url);
$data = $this->curl_get_contents($url);
$user_data = json_decode($data , true);
echo "<pre>";print_r($user_data);die;
$username = $user_data['login'];
$emails = file_get_contents("https://api.github.com/user/emails?access_token=$access_token");
$emails = json_decode($emails , true);
$email = $emails[0];
$signup_data = array(
'username' => $username ,
'email' => $email ,
'source' => 'github' ,
);
echo "<pre>";
print_r($signup_data);die;
}
function curl_get_contents($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
我使用cakephp框架。如果我使用file_get_contents($ url)。GitHub oauth api返回無法打開流:HTTP請求失敗! HTTP/1.0 403禁止使用file_get_contents
然後我得到以下警告..
的file_get_contents(https://api.github.com/user?access_token=dec39b61a997d1509c03a7128573187f1ed02684):未能打開流:HTTP請求失敗! HTTP/1.0 403禁止
但作爲建議在這個環節:file_get_contents returns 403 forbidden
我用捲曲,使用此之後,當我打印$user_data
可變我有空白頁。沒有錯誤沒有警告,但沒有檢索到數據。
如果我複製此網址並將其放置在瀏覽器中直接,我感到我需要那麼請建議我在哪裏,我錯了的所有信息..
感謝
您可以輕鬆地使用CakePHP httpsocket http://book.cakephp.org/2.0/en/core創建腳本-utility的庫/ httpsocket.html – Salines 2014-11-02 13:10:58