Php 如何在响应中仅获取JSON
当我通过Chrome浏览器调试站点时,会得到JSON响应。但是,当我试图通过PHP执行此操作时,会收到一条错误消息Php 如何在响应中仅获取JSON,php,Php,当我通过Chrome浏览器调试站点时,会得到JSON响应。但是,当我试图通过PHP执行此操作时,会收到一条错误消息 failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found 谢谢你的帮助 例如: 在Chrome中要做的事情: 转到第页:在底部你会看到一个按钮“Wiecej-ofert”。单击后,您将在调试中看到: http://gruper.pl/DataProvider.php?cityId=51&categ
failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found
谢谢你的帮助
例如:
在Chrome中要做的事情:
转到第页:在底部你会看到一个按钮“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>
“获取”
),
);
$context=stream\u context\u create($options);
$result=(文件内容($url,false,$context));
?>
我认为Chrome正在自动将代码转换为json。在PHP中使用json\u encode()可以更加明确 尝试使用json\u编码
$result = file_get_contents($url, false, $context);
echo json_encode($result);
请先检查PHP配置,确保
allow\u url\u fopen
[1]设置为“开”,如果必须更改设置,请不要忘记重新启动Apache(或您使用的任何Web服务器)。接下来,使用初始帖子中的URL并检查是否使用内联调试获得预期结果:
$url= 'http://gruper.pl/DataProvider.php?cityId=51&categoryId=0&mainNaviId=1&showBTile=true&page=1';
$contents= file_get_contents($url);
print 'Content returned: '.$contents.'---'."\n";
接下来,我们将检查返回的内容是否包含正确的JSON值,添加以下代码:
try {
$arrConverted= json_decode($contents);
print 'JSON-decoded representation:';
print_r($arrConverted);
} catch (Exception $ex) {
print 'JSON-decoding failed, the error was: '.$ex->getMessage();
}
您通常不需要创建上下文流来抓取网页,大多数函数中都内置了对FTP和HTTP等默认协议的支持[2]
让我们知道你是如何做到这一点的
[1]
[2] 您所说的JSON响应只是Chrome(以及其他浏览器)显示响应标题的方式,JSON看起来与此示例类似:重新表述您的问题-不清楚您的问题是什么。您能再次检查吗?我放置了新版本。可能重复的请两次不要问相同的问题。您可以再次检查吗?我要更改描述。非常感谢。