2014-03-04 85 views
0

當輸入以下爲我的終端翻譯命令行卷曲到PHP

curl -F 'client_id=[clientID]' -F 'client_secret=[clientSecret]' -F 'grant_type=authorization_code' -F 'redirect_uri=[redirectURI]' -F 'code=[code]' https://api.instagram.com/oauth/access_token 

我得到

{"code": 400, "error_type": "OAuthException", "error_message": "Invalid Client ID"} 

但是,當我嘗試實現使用相同的結果(所有變量都設置了。只是一種不必要的,包括他們)

$url = "https://api.instagram.com/oauth/authorize"; 

$data = array('client_id' => '['.$client_id.']', 
      'redirect_uri' => '['.$redirect_uri.']', 
      'scope' => '[relationships]', 
      'response_type' => '[code]'); 
//$fields = http_build_query($data, '', '&'); 

$c = curl_init();  
curl_setopt($c, CURLOPT_URL, $url); 
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($c, CURLOPT_POST, count($data)); 
curl_setopt($c, CURLOPT_POSTFIELDS, $data); 
curl_setopt($c, CURLOPT_HTTPHEADER, array('Accept: application/json')); 
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($c, CURLOPT_VERBOSE, 1); 
curl_setopt($c, CURLOPT_HEADER, true); 
$control = curl_exec($c); 

echo '<pre>'; 
print_r($control); 
echo '</pre>'; 

我得到

HTTP/1.1 100 Continue 
HTTP/1.1 302 Moved Temporarily 
Content-Type: text/html 
Date: Tue, 04 Mar 2014 18:56:59 GMT 
Location: https://instagram.com/oauth/authorize 
Server: nginx 
Content-Length: 154 
Connection: keep-alive 

HTTP/1.1 302 FOUND 
Cache-Control: private, no-cache, no-store, must-revalidate 
Content-Language: en 
Content-Type: text/html; charset=utf-8 
Date: Tue, 04 Mar 2014 18:56:59 GMT 
Expires: Sat, 01 Jan 2000 00:00:00 GMT 
Location: https://instagram.com/accounts/login/?next=/oauth/authorize 
Pragma: no-cache 
Server: nginx 
Set-Cookie: csrftoken=28c6b04e3e73745694d9fd4388acd8dd; expires=Tue, 03-Mar-2015 18:56:59 GMT; Max-Age=31449600; Path=/ 
Set-Cookie: mid=UxYh-wAEAAFN3ZUutKcww23Vw_jd; expires=Mon, 27-Feb-2034 18:56:59 GMT; Max-Age=630720000; Path=/ 
Set-Cookie: ccode=US; Path=/ 
Vary: Cookie, Accept-Language 
Content-Length: 0 
Connection: keep-alive 

HTTP/1.1 200 OK 
Cache-Control: private, no-cache, no-store, must-revalidate 
Content-Language: en 
Content-Type: text/html 
Date: Tue, 04 Mar 2014 18:56:59 GMT 
Expires: Sat, 01 Jan 2000 00:00:00 GMT 
Last-Modified: Tue, 04 Mar 2014 18:56:59 GMT 
Pragma: no-cache 
Server: nginx 
Set-Cookie: csrftoken=db8986eddf5ef57b2d080c7680ef614d; expires=Tue, 03-Mar-2015 18:56:59 GMT; Max-Age=31449600; Path=/ 
Set-Cookie: mid=UxYh-wAEAAGxzegm0WQTbJBav8qr; expires=Mon, 27-Feb-2034 18:56:59 GMT; Max-Age=630720000; Path=/ 
Set-Cookie: ccode=US; Path=/ 
Vary: Cookie, Accept-Language, Accept-Encoding 
X-Frame-Options: SAMEORIGIN 
Content-Length: 10841 
Connection: keep-alive 

東西似乎與我在我的PHP代碼中使用cURL的方式。有什麼建議麼?

+2

的'-F'選項對應於'CURLOPT_POSTFIELDS',而不是URL參數。 – Barmar

+0

另外,如果你想建立一個查詢字符串,使用'http_build_query()'。 – Brad

回答

0

網址爲:

$url = "https://api.instagram.com/oauth/authorize"; 

對於之後的參數,因爲你是模仿捲曲

$param = array(
    'client_id' => '[clientID]', 
    'client_secret' => '[clientSecret]', 
    'grant_type' => 'authorization_code', 
    'redirect_uri' => '[redirectURI]', // you may need to urlencode() this one! 
    'code' => '[code]' 
); 
curl_setopt($c, CURLOPT_POST, 1); 
curl_setopt($c, CURLOPT_POSTFIELDS, $param); 

-F不使用http_build_query()$param這裏,這將幫助你處理https鏈接。

curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false); 

如果你需要更多的調試,使之:

curl_setopt($c, CURLOPT_VERBOSE, true); 
+0

我更新了我的答案,即使在聽取您的建議之後,它仍然無法正常工作。 – Lance

+0

告訴你,不要使用'http_build_query'。直接在$ CURLOPT_POSTFIELDS中使用$ data數組。它會爲你工作。 –

+0

再次更新。這就是我現在得到的迴應 – Lance