2017-06-17 108 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; 
} 

?> 
相關問題