2014-02-24 113 views
0

當我通過Chrome瀏覽器調試一個網站時,我得到了JSON響應。但是當我嘗試通過PHP來做到這一點時,我收到一條錯誤消息。如何獲得響應的JSON

無法打開流:HTTP請求失敗! HTTP/1.0 404找不到

感謝您的幫助。

例如:

事情在Chrome的事:

轉到頁:http://gruper.pl/warszawa,並在底部你會看到一個按鈕「更多信息ofert」。單擊後,您將在調試看到:

http://gruper.pl/DataProvider.php?cityId=51&categoryId=0&mainNaviId=1&showBTile=true&page=1 

和響應:

[{"ID_PAGE":"59199","ID_CITY":"3952","main_city":"3952","date_start":"2014-02-23 18:00:00","date_end":"2014-03-01 23:59:00","price"..... 

是否有可能得到相同的PHP?

我的代碼是:

<?php 

$url = 'http://gruper.pl/DataProvider.php?cityId=51&categoryId=0&mainNaviId=1&showBTile=true&page=1'; 

// use key 'http' even if you send the request to https://... 
$options = array(
    'http' => array(
     'header' => "Content-type: application/x-www-form-urlencoded\r\n" . 
         "Accept:application/json\r\n" . 
         "Accept-Encoding:gzip,deflate,sdch\r\n" . 
         "X-Requested-With:XMLHttpRequest\r\n", 
     'method' => 'GET' 
    ), 
); 

$context = stream_context_create($options); 
$result = (file_get_contents($url, false, $context)); 

?> 
<html> 

<head> 
<meta charset="UTF-8"> 
</head> 

</html> 
+0

第一個標題後面有一個逗號','。用句號'.'代替它,看看它是否有幫助。我的猜測是,它會的,因爲最重要的標題最有可能是最後的標題。這是一個鬼鬼祟祟的bug;很難檢測,因爲它沒有語法錯誤。 –

+0

我改變了它,但它不是問題。 問題在於:url: http://gruper.pl/DataProvider.php?cityId=51&categoryId=0&mainNaviId=1&showBTile=true&page=1 即使在瀏覽器中也不起作用。 – user3345547

+0

空白頁?你是否有錯誤報告以防萬一? – 2014-02-24 12:24:30

回答

1

它看起來像URL將返回404 HTTP狀態代碼,除非這些標頭設置:

X-Requested-With: XMLHttpRequest 
Referer: http://gruper.pl/warszawa 

那麼這將工作:

<?php 

$url = 'http://gruper.pl/DataProvider.php?cityId=51&categoryId=0&mainNaviId=1&showBTile=true&page=1'; 

// use key 'http' even if you send the request to https://... 
$options = array(
    'http' => array(
     'header' => "X-Requested-With: XMLHttpRequest\r\n" . 
        "Referer: http://gruper.pl/warszawa" 
    ) 
); 

$context = stream_context_create($options); 
$result = (file_get_contents($url, false, $context)); 

echo $result; 

?> 
+0

謝謝。工作:) – user3345547