【问题标题】:HTTP curl failed 403 but success with GuzzleHTTP curl 失败 403 但 Guzzle 成功
【发布时间】:2018-11-05 13:38:50
【问题描述】:

我正在尝试使用 Instagram API 来获取用户供稿。使用正常的卷曲我得到 403,但使用 guzzle 可以正常工作。我不想使用第 3 方。有什么方法可以让它与 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();

即使使用邮递员,我也无法让它工作!那么,需要什么额外的东西才能让它发挥作用呢?

更新

仍然需要通过邮递员获得成功:

【问题讨论】:

    标签: php curl postman guzzle http-get


    【解决方案1】:

    您的标题格式不正确。来自 PHPs curl_setopt 页面:

    CURLOPT_HTTPHEADER 要设置的 HTTP 标头字段数组,在 format array('Content-type: text/plain', 'Content-length: 100')

    把你的$hArray改成这个就可以了:

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

    【讨论】:

    • 我仍然需要通过邮递员获得成功,你能帮忙吗?我已经更新了我的问题。
    • @HamzehSoboh 我建议你在 SO 上为此打开一个新问题,这个问题是关于“HTTP curl failed”,已解决。
    猜你喜欢
    • 2016-09-19
    • 2020-10-06
    • 2021-12-08
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多