简体   繁体   中英

How to get JSON in response

When I debug a site via Chrome browser I get JSON response. But when I try to do this via PHP I get an error message.

failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found

Thanks for any help.

For example:

Things to do in Chrome:

Go to page: http://gruper.pl/warszawa and on the bottom you will see a button "Wiecej ofert". After click you will see in a debug:

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

and response:

[{"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".....

Is there any possibility to get the same in PHP?

My code is:

<?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>

It looks like that URL will return a 404 HTTP status code unless these headers are set:

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

So this will work:

<?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;

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM