2

尝试获取类型为 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   

请帮忙

谢谢

4

2 回答 2

1

从给定 URL 返回的数据是无效的 JSON(它给出的一些值是NaN,这在 JavaScript 对象文字中有效,但在 JSON 中无效)。您需要获取源来修复其中的错误(或者在下载数据后尝试自己修补它们)。

于 2013-09-29T19:40:29.150 回答
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);
于 2013-09-29T19:52:16.940 回答