2014-11-02 118 views
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可变我有空白页。没有错误没有警告,但没有检索到数据。

如果我复制此网址并将其放置在浏览器中直接,我感到我需要那么请建议我在哪里,我错了的所有信息..

感谢

+0

您可以轻松地使用CakePHP httpsocket http://book.cakephp.org/2.0/en/core创建脚本-utility的库/ httpsocket.html – Salines 2014-11-02 13:10:58

回答

0

尝试下面我的代码 - :

if(isset($_GET['code'])) 
    { 
      $code = $_GET['code']; 
      $post = http_build_query(array(
       'client_id' => 'my_client_id', 
       'redirect_url' => 'redirect_url', 
       'client_secret' => 'client_secret', 
       'code' => $code, 
      )); 

      $context = stream_context_create(
       array(
        "http" => array(
         "method" => "POST", 
         'header'=> "Content-type: application/x-www-form-urlencoded\r\n" . 
            "Content-Length: ". strlen($post) . "\r\n". 
            "Accept: application/json" , 
         "content" => $post, 
        ) 
       ) 
      ); 

      $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']; 
      $scope = $r['scope']; 

      /*- Get User Details -*/ 
      $url = "https://api.github.com/user?access_token=".$access_token.""; 
      $options = array('http' => array('user_agent'=> $_SERVER['HTTP_USER_AGENT'])); 
      $context = stream_context_create($options); 
      $data = file_get_contents($url, false, $context); 
      $user_data = json_decode($data, true); 
      $username = $user_data['login']; 

      /*- Get User e-mail Details -*/     
      $url = "https://api.github.com/user/emails?access_token=".$access_token.""; 
      $options = array('http' => array('user_agent'=> $_SERVER['HTTP_USER_AGENT'])); 
      $context = stream_context_create($options); 
      $emails = file_get_contents($url, false, $context); 
      $email_data = json_decode($emails, true); 
      $email_id = $email_data[0]['email']; 
      $email_primary = $email_data[0]['primary']; 
      $email_verified = $email_data[0]['verified']; 

    } 

错误:无法打开流:HTTP请求失败! HTTP/1.0 403 禁止

此错误要解决下面的代码: -

$url = "https://api.github.com/user?access_token=".$access_token.""; 
    $options = array('http' => array('user_agent'=> $_SERVER['HTTP_USER_AGENT'])); 
    $context = stream_context_create($options); 
    $data = file_get_contents($url, false, $context); 
    $user_data = json_decode($data, true); 
    $username = $user_data['login']; 
相关问题