HTTP curl 失败 403 但 Guzzle 成功
HTTP curl failed 403 but success with Guzzle
我正在尝试使用 Instagram API 来获取用户供稿。使用正常卷曲我得到 403,但使用 guzzle 效果很好。我不想使用第三方。有什么方法可以让它与 curl 一起工作?
这是我的代码:
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 做了额外的事情才能让它发挥作用?
更新
还需要通过 postman 获得成功:
您的 headers 格式不正确。来自 PHPs curl_setopt 页面:
CURLOPT_HTTPHEADER An array of HTTP header fields to set, in the
format array('Content-type: text/plain', 'Content-length: 100')
将你的 $hArray
改成这个,你应该没问题:
$hArray = [
'User-Agent: '.USER_AGENT,
'X-Requested-With: XMLHttpRequest',
'X-Instagram-Gis: cc2796cff28f2a3d71c73a4b61f62d84'
];
我正在尝试使用 Instagram API 来获取用户供稿。使用正常卷曲我得到 403,但使用 guzzle 效果很好。我不想使用第三方。有什么方法可以让它与 curl 一起工作?
这是我的代码:
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 做了额外的事情才能让它发挥作用?
更新
还需要通过 postman 获得成功:
您的 headers 格式不正确。来自 PHPs curl_setopt 页面:
CURLOPT_HTTPHEADER An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100')
将你的 $hArray
改成这个,你应该没问题:
$hArray = [
'User-Agent: '.USER_AGENT,
'X-Requested-With: XMLHttpRequest',
'X-Instagram-Gis: cc2796cff28f2a3d71c73a4b61f62d84'
];