簡體   English   中英

如何獲取JSON作為響應

[英]How to get JSON in response

通過Chrome瀏覽器調試網站時,會收到JSON響應。 但是,當我嘗試通過PHP執行此操作時,我收到一條錯誤消息。

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

謝謝你的幫助。

例如:

Chrome的活動:

轉到頁面: http ://gruper.pl/warszawa,在底部,您將看到一個按鈕“ Wiecej 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>

除非設置了以下標頭,否則該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;

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM