php 为什么curl给出1020错误,而 Postman 给出响应[重复]

uemypmqf  于 2023-02-21  发布在  PHP
关注(0)|答案(1)|浏览(362)

此问题在此处已有答案

curl 1020 error when trying to scrape page using bash script(1个答案)
昨天关门了。
'我已经发送了一个GET(也POST)方法从API获取数据,但响应是错误1020,同时Post man尝试了,并给出了响应,然后这里是什么错误。由于安全原因,我无法共享API URL。

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => 'url_with_parameter',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_HTTPHEADER => array(
        'Cookie: PHPSESSID=f1r22a2srpgamnidkflgqsuem2; csrf_token=c31d6332c0066fa652b79948c43e5dd7_d5ec9637ea9c6914f3cebeab7fa4ad4d',
        'Host' => "localhost",
        'User-Agent' => 'PostmanRuntime/7.31.0',
        'Accept' => '*/*',
        'Accept-Encoding' => 'gzip, deflate, br',
        'Connection' => 'keep-alive'
    ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
6jjcrrmo

6jjcrrmo1#

当curl返回1020错误时,通常意味着请求被安全设置阻止,安全设置可能包括验证码或浏览器检查等挑战。
尝试将UA头添加到curl请求中,例如

$headers = [
    'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36',
];

curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);

相关问题