Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php解析来自google directions的json数据_Php - Fatal编程技术网

php解析来自google directions的json数据

php解析来自google directions的json数据,php,Php,我试图解析来自google directions的json数据,但似乎无法访问任何值。下面是php代码 $url = "http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles&sensor=false"; $jsonfile = json_encode(file_get_contents($url)); $jsondata = json_deco

我试图解析来自google directions的json数据,但似乎无法访问任何值。下面是php代码

$url = "http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles&sensor=false";

$jsonfile = json_encode(file_get_contents($url));

$jsondata = json_decode($jsonfile);

echo $jsondata->distance;

以下是(部分)json数据返回:

{ "routes" : [ { "bounds" : { "northeast" : { "lat" : 41.90085000000001, "lng" : -87.62979000000001 }, "southwest" : { "lat" : 34.052360, "lng" : -118.243560 } }, "copyrights" : "Map data ©2012 Google", "legs" : [ { "distance" : { "text" : "2,015 mi", "value" : 3243390 }, "duration" : { "text" : "1 day 8 hours", "value" : 114318 }, "end_address" : "Los Angeles, CA, USA", "end_location" : { "lat" : 34.052360, "lng" : -118.243560 }, "start_address" : "Chicago, IL, USA", "start_location" : { "lat" : 41.87811000000001, "lng" : -87.62979000000001 }, "steps" : [ { "distance" : { "text" : "0.2 mi", "value" : 269 }, "duration" : { "text" : "1 min", "value" : 34 }, "end_location" : { "lat" : 41.87570, "lng" : -87.62969000000001 }, "html_instructions" : "Head \u003cb\u003esouth\u003c/b\u003e on \u003cb\u003eS Federal St\u003c/b\u003e toward \u003cb\u003eW Van Buren St\u003c/b\u003e", "polyline" : { "points" : "eir~FdezuOhFIF?HAdFG" }, "start_location" : { "lat" : 41.87811000000001, "lng" : -87.62979000000001 }, "travel_mode" : "DRIVING" }, { "distance" : { "text" : "0.2 mi", "value" : 328 }, "duration" : { "text" : "1 min", "value" : 51 }, "end_location" : { "lat" : 41.875680, "lng" : -87.63366000000002 }, "html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eW Congress Pkwy\u003c/b\u003e", "polyline" : { "points" : "czq~FpdzuO?jE@nEBfGAtB" }, "start_location" : { "lat" : 41.87570, "lng" : -87.62969000000001 }, "travel_mode" : "DRIVING" }, { "distance" : { "text" : "14.0 mi", "value" : 22564 }, "duration" : { "text" : "17 mins", "value" : 1031 }, "end_location" : { "lat" : 41.873870, "lng" : -87.903170 }, "html_instructions" : "Continue onto \u003cb\u003eI-290 W\u003c/b\u003e", "polyline" : { "points" : "_zq~Fj}zuOCtD?fA?f@?pC?P?T@V@zJ?@@T?J?J?F?N?@?J?J?N?p@@xA?h@?B?~A@xD?h@?lB?n@@n@?n@?B@~G?f@A\\EvAGnBEz@EpA?JBlC?F?F?T?B?f@HtM?R?J?t@?|CCvDAb@E`EGlDKzCCd@OrBa@rFGfAAd@?HAp@?`ADbJDzJ?fB?dENdFB~N?d@?RFlH@~C?J@~A?H?XFrKAx@@l@?n@@^@bANnQ?rAAnMCnC@tH@x@FxJ?hB@tM?j@?p@BxFLrT?B?H?jQBbG@|AHxLBVJ~CLfC\\|EHtAHtAVpDf@tF\\dFTxCXhDVzBNtCFlEB|HBvEDlRAhC?xANxU@^?~A?nADxW@vA?l@Dv[?|@?\\@bCHdI@z@ZxDh@zDRx@T|@t@dCPf@bBlEf@dB^vA^jCR`CDv@?LHfBH`X?z@F|V@pF?`@L|Y@v@@dKDbO?jEDdHZjHR`EBd@D`@h@~IHpB@p@@n@?p@BjACvFE`IMrIQzIKfEc@tQEjCCn@}@dUSjFCf@mA~[Ar@QlMObGWlEEx@sB`_@Ep@aBt\\EdA?FcAbU?XCrCKnC?zBR|LB`@B\\?Dz@hG@F@Ft@vDJl@pBfMnAtHZ|ERrEJhTH|GLvJ^rHr@|HX`G@`FLnX@`B?`A@|CAbCRnT?BJ`OHrJDbC@r@XzZ?@DfHMdIe@bHIhD@zHPhP?@@V@fBN|O?T@j@DnEHpQFlGDtCDtEDrEFlEt@jJF~@`@dDPtAXtCd@`IFlC@j@?j@?hAHpLB~C@bDA|DErBCl@Av@O`C]~DWzB[lB[fB]zAo@dCc@~Am@jBo@bBoCxG?@Sd@g@vAY~@St@W|@_@bBUhAYdB]vBMhA[vCKtAEbAIxA?PAXCb@AhA" }, "start_location" : { "lat" : 41.875680, "lng" : -87.63366000000002 }, "travel_mode" : "DRIVING" }, { "distance" : { "text" : "140 mi", "value" : 224957 }, "duration" : { "text" : "2 hours 13 mins", "value" : 8008 }, "end_location" : { "lat" : 41.53651000000001, "lng" : -90.32902000000001 }

返回的错误如下所示:

( ! ) Notice: Trying to get property of non-object in C:\wamp2\www\phpAcademy\GoogleDirectionsApi\TMP25mnfz82oi.php on line 8
Call Stack
#   Time    Memory  Function    Location
1   0.0004  366752  {main}( )   ..\TMP25mnfz82oi.php:0

谷歌已经返回了JSON,所以你不需要先对它进行编码。其次,“距离”键在响应结构中更深:

 $origin      = "Rome, Italy";
 $destination = "Venice, Italy";
 $key = "YOUR-OWN-KEY";
 $url = "https://maps.googleapis.com/maps/api/directions/json?origin=".urlencode($origin).",IL&destination=" . urlencode( $destination) . "&sensor=false&key=" . $key;
 $jsonfile = file_get_contents($url);
 $jsondata = json_decode($jsonfile);
 echo $jsondata->routes[0]->legs[0]->distance;

您可以轻松获取json或数组格式的所有数据:

$url=”“

$jsonfile=file\u get\u contents($url)

$jsondata=json_decode($jsonfile)

echo$jsondata->routes[0]->legs[0]->distance

如果发生此错误:

可捕获的致命错误:类stdClass的对象无法转换为字符串


而不是用于输出print_r()函数

我赞成删除这个问题,因为这主要是一个疏忽。