GET curl请求仅适用于邮递员,不适用于php

时间:2018-09-01 16:04:41

标签: php curl

我正在尝试获取html作为响应,但它仅适用于邮递员,不适用于php。在php中,我得到了不同的响应,例如“出了点问题……”

我想念什么?

这是代码:

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://www.avnet.com/shop/SearchDisplay?searchTerm=LMK316BJ476ML-T&countryId=apac&deflangId=-1&storeId=715839038&catalogId=10001&langId=-1&sType=SimpleSearch&resultCatEntryType=2&searchSource=Q&searchType=100&avnSearchType=all",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "Cache-Control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

邮递员回应

enter image description here

php响应

enter image description here

先谢谢了。

1 个答案:

答案 0 :(得分:2)

我在CURLOPT_HTTPHEADER周围玩耍,发现该站点正在验证2个标头,分别是User-Agent和Accept-Language。我正确地发送了它并且有效。请在下面找到标题

CURLOPT_HTTPHEADER => array(
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
"Accept-Language:en-US,en;q=0.5"
)

很高兴找出问题所在。