2014-01-21 32 views
0

這裏返回的記錄計數是從作品的linkedIn documentation一個簡單的查詢:我無法控制的通過LinkedIn的API

$groupData = $this->linkedin->fetch('GET', "/v1/groups/{id}/posts"); 

它返回10條記錄。但此刻我附上數和啓動參數,像這樣:

$groupData = $this->linkedin->fetch('GET', "/v1/groups/{id}/posts?count=20&start=0"); 

我得到這個錯誤:

A PHP Error was encountered 

Severity: Warning 

Message: file_get_contents(https://api.linkedin.com/v1/groups/{id}/posts&count=20&start=0?oauth2_access_token=xxxxx8&format=json): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request 

Filename: libraries/Linkedin.php 

Line Number: 85 

這裏是我的完整代碼:

class Auth extends CI_Controller { 

    function __construct() { 
     parent:: __construct(); 
     $this->load->library('linkedin'); // load library 
     session_name('linkedin'); 
     session_start(); 
    } 

    // linkedin login script 
    function index() { 
     // OAuth 2 Control Flow 
     if (isset($_GET['error'])) { 
      // LinkedIn returned an error 
      // load any error view here 
      exit; 
     } elseif (isset($_GET['code'])) { 
      // User authorized your application 
      if ($_SESSION['state'] == $_GET['state']) { 
       // Get token so you can make API calls 
       $this->linkedin->getAccessToken(); 
      } else { 

       // CSRF attack? Or did you mix up your states? 
       exit; 
      } 
     } else { 
      if ((empty($_SESSION['expires_at'])) || (time() > $_SESSION['expires_at'])) { 
       // Token has expired, clear the state 
       $_SESSION = array(); 
      } 
      if (empty($_SESSION['access_token'])) { 
       // Start authorization process 
       $this->linkedin->getAuthorizationCode(); 
      } 
     } 

     // this is where I am fetching linkedIn data 
     $groupData = $this->linkedin->fetch('GET', "/v1/groups/{id}/posts?count=20&start=0"); 

     // this is where I am sending the data to the idea model to be saved 
     if ($groupData) { 
     var_dump($groupData); exit(); 
     // foreach ($groupData->values as $data) { 
     // var_dump($data->creator->firstName); exit(); 
     // } 

     $this->load->model('idea_model'); 
     $this->idea_model->store_ideas($groupData); 

     } else { 
     // linked return an empty array of profile data 
     } 

    } 

} 

LinkedIn的圖書館是代碼示例由linkedIn在其文檔中給出:

<?php 

defined('BASEPATH') OR exit('No direct script access allowed'); 

/** 
* CodeIgniter Linked API Class 
* 
* 
* @package   CodeIgniter 
* @subpackage  Libraries 
* @category  Libraries 
* @author   Muhamamd Hafeez 
*/ 
class Linkedin { 

    function __construct(){ 

    } 

    public function getAuthorizationCode() { 
    $params = array('response_type' => 'code', 
     'client_id' => API_KEY, 
     'scope' => SCOPE, 
     'state' => uniqid('', true), // unique long string 
     'redirect_uri' => REDIRECT_URI, 
    ); 
    // Authentication request 
    $url = 'https://www.linkedin.com/uas/oauth2/authorization?' . http_build_query($params); 

    // Needed to identify request when it returns to us 
    $_SESSION['state'] = $params['state']; 

    // Redirect user to authenticate 
    header("Location: $url"); 
    exit; 
    } 

    public function getAccessToken() { 
    $params = array('grant_type' => 'authorization_code', 
     'client_id' => API_KEY, 
     'client_secret' => API_SECRET, 
     'code' => $_GET['code'], 
     'redirect_uri' => REDIRECT_URI, 
    ); 
    // Access Token request 
    $url = 'https://www.linkedin.com/uas/oauth2/accessToken?' . http_build_query($params); 

    // Tell streams to make a POST request 
    $context = stream_context_create(
      array('http' => 
       array('method' => 'POST', 
       ) 
      ) 
    ); 

    // Retrieve access token information 
    $response = file_get_contents($url, false, $context); 

    // Native PHP object, please 
    $token = json_decode($response); 

    // Store access token and expiration time 
    $_SESSION['access_token'] = $token->access_token; // guard this! 
    $_SESSION['expires_in'] = $token->expires_in; // relative time (in seconds) 
    $_SESSION['expires_at'] = time() + $_SESSION['expires_in']; // absolute time 
    return true; 
    } 

    public function fetch($method, $resource, $body = '') { 
    $params = array('oauth2_access_token' => $_SESSION['access_token'], 
     'format' => 'json', 
    ); 

    // Need to use HTTPS 
    $url = 'https://api.linkedin.com' . $resource . '?' . http_build_query($params); 
    // Tell streams to make a (GET, POST, PUT, or DELETE) request 
    $context = stream_context_create(
      array('http' => 
       array('method' => $method, 
       ) 
      ) 
    ); 

    // Hocus Pocus 
    $response = file_get_contents($url, false, $context); 

    // Native PHP object, please 
    return json_decode($response); 
    } 

} 

/* End of file Linked.php */ 
/* Location: ./application/libraries/linkedin.php */ 

請幫我解決這個問題。我究竟做錯了什麼?

+0

旁註:由於您使用的會議,我看沒有提及['在session_start() ;'](http://www.php.net/session_start)---如果你沒有包含它,會話需要工作。 –

+0

'session_start();''已包含 - 我將更新代碼以反映此 –

+0

看起來您發佈的錯誤來自您嘗試使用'/ v1/groups/{id}/posts&count = 20&start = 0「'作爲你的第二個參數,而不是'」/ v1/groups/{id}/posts?count = 20&start = 0「'(注意&而不是?)。 –

回答

1

問題是你的參數列表有兩個兩個?'s。我想改變fetch方法採取一個可選$params參數:

public function fetch($method, $resource, $params = array(), $body = '') { 
    // Cast, just in case 
    $params = (array)$params; 
    // Add mandatory parameters 
    $params['oauth2_access_token'] = $_SESSION['access_token']; 
    $params['format'] = 'json'; 

    // Need to use HTTPS 
    $url = 'https://api.linkedin.com' . $resource . '?' . http_build_query($params); 
    // Tell streams to make a (GET, POST, PUT, or DELETE) request 
    $context = stream_context_create(
      array('http' => 
       array('method' => $method, 
       ) 
      ) 
    ); 

    // Hocus Pocus 
    $response = file_get_contents($url, false, $context); 

    // Native PHP object, please 
    return json_decode($response); 
} 

,並調用它像這樣:

$groupData = $this->linkedin->fetch('GET', "/v1/groups/{id}/posts", array("count" => 20, "start" => 0)); 
+0

這工作:只是一個小的更正 - '$ params ['format'] = $ _SESSION ['json'];'應該是'$ params ['format'] ='json'; ' –

+1

好的。修復了答案。 –