简体   繁体   English

如何获取JSON作为响应

[英]How to get JSON in response

When I debug a site via Chrome browser I get JSON response. 通过Chrome浏览器调试网站时,会收到JSON响应。 But when I try to do this via PHP I get an error message. 但是,当我尝试通过PHP执行此操作时,我收到一条错误消息。

failed to open stream: HTTP request failed! 无法打开流:HTTP请求失败! HTTP/1.0 404 Not Found 找不到HTTP / 1.0 404

Thanks for any help. 谢谢你的帮助。

For example: 例如:

Things to do in Chrome: Chrome的活动:

Go to page: http://gruper.pl/warszawa and on the bottom you will see a button "Wiecej ofert". 转到页面: http ://gruper.pl/warszawa,在底部,您将看到一个按钮“ 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? 有可能在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: 除非设置了以下标头,否则该URL看起来将返回404 HTTP状态代码:

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;

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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