HTTP curl失败了403但是Guzzle成功了

时间:2018-05-26 09:33:09

标签: php curl postman guzzle http-get

我正在尝试使用Instagram API来获取用户Feed。正常的卷曲我得到403,但它使用guzzle工作正常。我不想使用第三方。任何方法让它与卷曲一起工作?

这是我的代码:

const INSTAGRAM_ENDPOINT = 'https://www.instagram.com/';
const USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36';
const QUERY_HASH = '42323d64886122307be10013ad2dcc44';

$variables = [
    'id' => '232192182',
    'first' => '12',
    'after' => 'AQCWAl5VxOspHxLFduBo0kmKu0ILD-jynobaAJQBvRz8S1Vu1rDpz5KnyTADU4hxyOyHAsnrF_8RYXOYIXgwzbdUO41D2rfzL-PZN26HABDtoQ' // $this->endCursor,
];
$endpoint = INSTAGRAM_ENDPOINT . 'graphql/query/?query_hash=' . QUERY_HASH . '&variables=' . json_encode($variables);

$hArray = [
    'User-Agent' => USER_AGENT,
    'X-Requested-With' => 'XMLHttpRequest',
    'X-Instagram-Gis' => 'cc2796cff28f2a3d71c73a4b61f62d84'
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $hArray);
$server_output = curl_exec($ch);
curl_close($ch);
echo "<br>" . "<br>" . $server_output . "<br>" . "<br>";

响应:

HTTP/1.1 403 Forbidden Content-Type: text/html; charset=utf-8 x-robots-tag: noindex Cache-Control: private, no-cache, no-store, must-revalidate Pragma: no-cache Expires: Sat, 01 Jan 2000 00:00:00 GMT Vary: Cookie, Accept-Language Content-Language: en Date: Sat, 26 May 2018 09:22:28 GMT Strict-Transport-Security: max-age=86400 Set-Cookie: rur=FRC; Path=/ Set-Cookie: csrftoken=Vhh1ImCoyX1uMhfjZx65R9QbzewBsgzH; expires=Sat, 25-May-2019 09:22:28 GMT; Max-Age=31449600; Path=/; Secure Set-Cookie: mid=WwknVAAEAAFT3mN8ogObl6HSnQNy; expires=Fri, 21-May-2038 09:22:28 GMT; Max-Age=630720000; Path=/ Connection: keep-alive Content-Length: 0 

guzzle工作代码是:

$res = client->request('GET', $endpoint, ['headers' => $hArray]);
$data = (string)$res->getBody();

即使有邮差,我也无法工作!那么,什么guzzle做额外的东西让它工作?

更新

仍然需要与邮递员取得成功:

enter image description here

1 个答案:

答案 0 :(得分:2)

您的标头格式不正确。来自PHP curl_setopt页面:

  

CURLOPT_HTTPHEADER要设置的HTTP标头字段数组   格式数组(&#39;内容类型:text / plain&#39;,&#39;内容长度:100&#39;)

将您的$hArray更改为此,您应该可以:

$hArray = [
    'User-Agent: '.USER_AGENT,
    'X-Requested-With: XMLHttpRequest',
    'X-Instagram-Gis: cc2796cff28f2a3d71c73a4b61f62d84'
];