2012-02-11 4 views
4

Google 지침에서 json 데이터를 구문 분석하려고하지만 액세스 할 수없는 값이 있습니다. 다음은 PHP 코드입니다.php parse json data from google

$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" : "[email protected]" }, "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}[email protected][email protected]@[email protected]@[email protected][email protected]@[email protected][email protected][email protected][email protected]@[email protected][email protected][email protected][email protected]\\[email protected][email protected][email protected]?|[email protected]`[email protected]@[email protected][email protected]?`[email protected][email protected][email protected][email protected]@[email protected][email protected]@^@[email protected]@[email protected][email protected][email protected][email protected]?B?H?jQ[email protected]|AHxLBVJ~CLfC\\|[email protected]\\dFTxCXhDVzBNtCFlEB|[email protected]^[email protected][email protected][?|@?\\@[email protected]@[email protected]@T|@[email protected]@[email protected]^vA^jCR`[email protected]?LHfBH`[email protected]|[email protected]?`@L|[email protected]@@dKDbO?jEDdHZjHR`[email protected]`@[email protected][email protected]@@[email protected][email protected]`[email protected]@}@[email protected]~[[email protected]@sB`[email protected]@aBt\\EdA?FcAbU?XCrCKnC?zBR|LB`@B\\[email protected]@[email protected]@[email protected]|ERrEJhTH|GLvJ^[email protected]|HX`[email protected]`[email protected]`B?`[email protected]|CAbCRnT?BJ`[email protected]@[email protected]@[email protected][email protected]@[email protected]|[email protected]@[email protected][email protected]`@[email protected]`[email protected]@[email protected][email protected]|[email protected]@O`C]~DWzB[lB[fB][email protected]@[email protected]@[email protected]@[email protected][email protected]@W|@[email protected]]vBMhA[[email protected]" }, "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 
+0

주로이 부분은 감독이기 때문에이 질문을 삭제하겠습니다. – quickshiftin

답변

5

구글은 이미 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; 
+1

당신은 나를 때려! :) –

+0

Secator, 많은 도움을 주셔서 감사합니다. 매우 감사. – user615099

+0

당신은 환영합니다 :) –