这是我正在尝试的请求。但是url在浏览器中工作并返回HTML。 POSTMAN但不是php curl或命令行。
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://www.walmart.com/header?mobileResponsive=true",
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",
"postman-token: 04275e89-412a-edbf-a63d-d6ebe5c3c126"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
从命令行尝试相同的网址
curl --request GET \
--url 'http://www.walmart.com/header?mobileResponsive=true' \
--header 'cache-control: no-cache' \
--header 'postman-token: 301d71b1-fde5-a66f-2433-ed6baf9c8426'
由于
答案 0 :(得分:0)
如果您使用-v
尝试详细模式,您将看到请求从http重定向到https:
curl -v http://www.walmart.com/header?mobileResponsive=true
* Trying 23.200.157.25...
* Connected to www.walmart.com (23.200.157.25) port 80 (#0)
> GET /header?mobileResponsive=true HTTP/1.1
> Host: www.walmart.com
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Accept-Ranges: bytes
< Content-Length: 54
使用https位置:
curl "https://www.walmart.com/header?mobileResponsive=true"
或者,如果您希望curl在新位置执行新请求,请使用-L
(--location
):
curl -L "http://www.walmart.com/header?mobileResponsive=true"
注意:
-X/--request
,默认方法为GET
在您的PHP代码中,同样适用:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.walmart.com/header?mobileResponsive=true",
CURLOPT_RETURNTRANSFER => true
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>