2017-06-17 106 views
-1

这是我正在尝试的请求。但该网址的作品,并返回HTML浏览器& POSTMAN,但不是在PHP卷曲或命令行。在Postman中工作的url请求,但不在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; 
} 

尝试相同的URL的命令行

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

如果您在详细模式下尝试-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" 

或者,如果你想卷曲在新的地点使用-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; 
} 

?> 
相关问题