2015-09-07 33 views
0

我嘗試做一些簡單的LinkedIn的API調用,從授權。 爲了測試目的,我創建了這個$ url,我知道它的工作原理並打開它。PHP - Authorizate在LinkedIn API

但有一個區別,如果我通過代碼調用此URL的下方,輸入我的LinkedIn的憑據,我全自動重定向到

它看起來像更換了一些LinkedIn URL「本地主機」,而不是和我確定它不是我的redirect_uri,我沒有在任何地方定義它。

我找不到爲什麼發生這種情況,如果我直接在瀏覽器中打開頁面的原因登錄後(只需複製從$ URL粘貼URL),我重定向到example.com所應當。

我的代碼:

//define path 

$url = "https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=MyIdHere&redirect_uri=https%3A%2F%2Fwww.example.com%2Fauth%2Flinkedin&state=DCEeFWf45A53sdfKef424&scope=r_basicprofile"; 

// set up the curl resource 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_VERBOSE, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                   
    'Content-Type: application/x-www-form-urlencoded'   
));  

// execute the request 
$output = curl_exec($ch); 

// output the auth information - includes the header 
var_dump($output); 

// close curl resource to free up system resources 
curl_close($ch); 
?> 

感謝您的任何建議。

回答

0

你捲曲的代碼不會跟蹤重定向作爲瀏覽器呢,所以沒有後續在由LinkedIn提供的響應Location頭。另見Make curl follow redirects?

但是:你也應該沒有真正嘗試修復,自OAuth 2.0用戶流是指通過瀏覽器的用戶交互和逆向工程在PHP會在某個時候突破。相反,您應該通過常規的基於瀏覽器的流程獲取訪問令牌,並在您的PHP代碼中使用該令牌訪問LinkedIn API。