尝试使用text / x-json
类型获得响应和json解码它(失败)
这是我的代码段:
<?php
//prepare URL (in this example send WAZE web site a route calculation
$url = "http://www.waze.com/RoutingManager/routingRequest?from=x%3A-73.8876574+y%3A40.7664011+bd%3Atrue&to=x%3A-73.7721035+y%3A40.7486434+bd%3Atrue&returnJSON=true&returnGeometries=true&returnInstructions=true&timeout=60000&nPaths=2";
//send GET request to WAZE web site
$response = file_get_contents($url);
//All this isnt working
//$response = utf8_encode($response);
//$response = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $response);
//$response = preg_replace('/.+?({.+}).+/','$1',$response);
//go json
$responseJson = json_decode($response);
// this isnt working 2
//$responseJson = json_decode($response,true);
//now $responseJson is null :(
HELP!
?>
这是我得到的响应标题:
$http_response_header Array [6]
0 (string:15) HTTP/1.1 200 OK
1 (string:25) Content-Type: text/x-json
2 (string:35) Date: Sun, 29 Sep 2013 19:36:08 GMT
3 (string:19) Server: nginx/1.4.1
4 (string:30) X-UA-Compatible: IE=EmulateIE7
5 (string:17) Connection: Close
请帮助
由于
答案 0 :(得分:1)
从给定URL返回的数据是无效的JSON(它提供了一些值为NaN
,这在JavaScript对象文字中有效,但在JSON中无效)。您需要获取源代码来修复其中的错误(或者在下载数据后尝试自己修补它们)。
答案 1 :(得分:0)
这样可以正常工作,但不确定每次都能正常工作。
$url = "http://www.waze.com/RoutingManager/routingRequest?from=x%3A-73.8876574+y%3A40.7664011+bd%3Atrue&to=x%3A-73.7721035+y%3A40.7486434+bd%3Atrue&returnJSON=true&returnGeometries=true&returnInstructions=true&timeout=60000&nPaths=2";
//send GET request to WAZE web site
$response = file_get_contents($url);
$response = str_replace("NaN",'"NaN"',$response);
$responseJson = json_decode($response,true);