将命令行cURL转换为PHP

时间:2014-03-04 17:47:44

标签: php curl

在我的终端中输入以下内容

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的方式似乎有些不对劲。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

网址为:

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

对于post参数,请不要在此http_build_query()上使用$param,因为您正在模仿卷曲的-F

$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);

这将有助于您处理https链接。

curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);

如果您需要更多调试,请启用它:

curl_setopt($c, CURLOPT_VERBOSE, true);