Show multiple api result photos on google map - php

Get 20 results of (example:School ) from google places api and i want to show photos on google map but some api results does not contain any image so how i can fix it. Error show: undefined index 'photos'.
if ($resp['status']=='OK') {
//Loop through get the multiple results
for ($i=0; $i < count($resp['results']) ; $i++) {
//Get the Important Data
$data['formatted_address'] = $resp['results'][$i]['formatted_address'];
$data['latitude'] = $resp['results'][$i]['geometry']['location']['lat'];
$data['longitude'] = $resp['results'][$i]['geometry']['location']
enter code here['lng'];
$data['icon'] = $resp['results'][$i]['icon'];
$data['name'] = $resp['results'][$i]['name'];
$data['height'] = $resp['results'][$i]['photos'][0]['height'];
$data['html_attributions'] = $resp['results'][$i]['photos'][0]
['html_attributions'][0];
$data['photo_reference'] = $resp['results'][$i]['photos'][0]
['photo_reference'];
$data['width'] = $resp['results'][$i]['photos'][0]['width'];
//Verify if Data Exist
if (!empty($data) && !empty ($data['latitude']) && !empty
($data['longitude'])) {
print_r($data);
}else{
echo "<h1 style=color:red;>Values Empty</h1>";
}
}//End for-loop
}else{
echo "<h1 style=color:red;>Status Error</h1>";
}
Get the result of schools from google places api the index[1] does not contain any image than how to show on map.
[results] => Array(
[0] => Array
(
[formatted_address] => Tariqabad Multan, Punjab, Pakistan
[geometry] => Array
(
[location] => Array
(
[lat] => 30.2073928
[lng] => 71.4530978
)
[viewport] => Array
(
[northeast] => Array
(
[lat] => 30.208742629893
[lng] => 71.454447629893
)
[southwest] => Array
(
[lat] => 30.206042970107
[lng] => 71.451747970107
)
)
)
[icon] =>
https://maps.gstatic.com/mapfiles/place_api/icons/school-71.png
[id] => de1405f04fffe5f2910789717344e513625aada3
[name] => Learner's Castle School Multan
[photos] => Array
(
[0] => Array
(
[height] => 730
[html_attributions] => Array
(
[0] => Faisal Mushtaq
)
[photo_reference] =>
CmRaAAAAvhDiC7bMqpf3F5lvLSUmDkv8aAqSF1AaC_EoeUhG
f40GCU3H0zaLOQZ96fRZxTJ9goxxh2C
CfJXtJyXfMOBBqPPsOAKenERrefpNwtdV07nDyPE62nSdz3aO7T1aQaMy
EhBQOTSeTEKvNxBCk5rxl7 FUGhQ9MSlemG_o0jxeYE9f0_RmtnQzrQ
[width] => 1296
)
)
[place_id] => ChIJqfKsD8IzOzkRomMlLG80aho
[plus_code] => Array
(
[compound_code] => 6F43+X6 Multan, Pakistan
[global_code] => 8J2H6F43+X6
)
[rating] => 5
[reference] => ChIJqfKsD8IzOzkRomMlLG80aho
[types] => Array
(
[0] => school
[1] => point_of_interest
[2] => establishment
)
)
[1] => Array
(
[formatted_address] => N Gulgasht Blvd, Basti Mehmood Kot,
Multan, Punjab 60000, Pakistan
[geometry] => Array
(
[location] => Array
(
[lat] => 30.2464553
[lng] => 71.483087
)
[viewport] => Array
(
[northeast] => Array
(
[lat] => 30.247805129893
[lng] => 71.484436829893
)
[southwest] => Array
(
[lat] => 30.245105470107
[lng] => 71.481737170107
)
)
)
[icon] =>
https://maps.gstatic.com/mapfiles/place_api/icons/school-71.png
[id] => 8648cd48c320f11b001a0eece814bd72abc07231
[name] => Zakariya Public School
[opening_hours] => Array
(
[open_now] => 1
)
[place_id] => ChIJ5c1N-YM0OzkRKKtxXEhilow
[plus_code] => Array
(
[compound_code] => 6FWM+H6 Multan, Pakistan
[global_code] => 8J2H6FWM+H6
)
[rating] => 4.4
[reference] => ChIJ5c1N-YM0OzkRKKtxXEhilow
[types] => Array
(
[0] => school
[1] => point_of_interest
[2] => establishment
)
)
[2] => Array
(
[formatted_address] => 17- Officers Colony, Officers Colony,
Multan, Punjab, Pakistan
[geometry] => Array
(
[location] => Array
(
[lat] => 30.2105142
[lng] => 71.4726681
)
[viewport] => Array
(
[northeast] => Array
(
[lat] => 30.211864029893
[lng] => 71.474017929893
)
[southwest] => Array
(
[lat] => 30.209164370107
[lng] => 71.471318270107
)
)
)
[icon] =>
https://maps.gstatic.com/mapfiles/place_api/icons/school-71.png
[id] => 8c809b4c5a4b8355899ddf6b6a7497bb8cb43be1
[name] => Jinnah Highs School System
[opening_hours] => Array
(
[open_now] => 1
)
[photos] => Array
(
[0] => Array
(
[height] => 1920
[html_attributions] => Array
(
[0] => A Google User
)
[photo_reference] =>
CmRZAAAAmQ8VIRfu9_s5UTrmBRwHpPWYuPjXWWcRj5KnWGwTy4yXo-
h2bWa8NSXrYSI8ukYD8nhfQRVxjxVKfWMxJlC4Xn8D9KBmoB7M9P
CZU5_XHvZ2heWSXPkqcGvjlcDiMalsEhA3VzcWjoDnL9Fs7Bs8PP
t4GhSePGS5CmCg0AXA8kEfl91iWotZgA
[width] => 2560
)
)
[place_id] => ChIJz4eKo_YzOzkRHvCbt43xkq8
[plus_code] => Array
(
[compound_code] => 6F6F+63 Multan, Pakistan
[global_code] => 8J2H6F6F+63
)
[rating] => 4.3
[reference] => ChIJz4eKo_YzOzkRHvCbt43xkq8
[types] => Array
(
[0] => school
[1] => point_of_interest
[2] => establishment
)
)

you should first check if photo key is exist in $resp['results'][$i] array by using isset function in php
for example
if(isset($resp['results'][$i]['photos']) && count($resp['results'][$i]['photos']) > 0)
{
// write your code
}
and also you should use phpenter code here count function to check $resp['results'][$i]['photos'] array has multi entry
so you can write $resp['results'][$i]['photos'][0] without have out of scope error

Related

getting array value from json array PHP

i am using google place json array
json array
i am using this code to get name of place
$string = file_get_contents("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&rankby=distance&types=food&key=AIzaSyCpOG91fkAzPTZiFq7G9HbLfbmVyW8Wgyc");
$json_a = json_decode($string, true);
echo print_r(array_values($json_a-> results[0]->name[0]));
but the result always { 1 } where the result should be Biaggio Cafe
here if i print out the array print_r($json_a)
Array ( [html_attributions] => Array ( ) [next_page_token] => CqQCFwEAAN1f0rbnMkVsdezGGV9kDlKiYdkDKgOSeaul7JL6GFEeLhV0-h3_WHa0NGbQ_here u_N7K3e7r5xxtW4FGjp6dHK4WKxUb1LPbmLq4KvybiR7QyYspK6wHC4In7Slnhj_0veXd25lruweEKFTmnu6fX0JUN3Lwtyz4KnQrnGeJHX7Ldr6oWJcSn-ilM2AXCQi0LnFza4dJmlvh62Eq583kQ5s1WQO8VrnQ8G66Yfy69X7Glbe0Pijj384gSWncN6kEbEUAw_PyHe4R3z50MRa336ZpzTxOulRwjV6NtNtUbn-j-omj0fb2Q0Yf6QQRrmiAaKx2QqYaN3AB7oH1YouLKLyGVzA3qOoIdYmI3OAztgkPKWUlNl5PzS3Ro7V8g-RdNN0hIQTtLlDEzmNPviMy1X28gkHBoUQxlMdi3EX_zrQzljRF12sjVab2c [results] => Array ( [0] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8669667 [lng] => 151.1958862 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.865664669709 [lng] => 151.19708633029 ) [southwest] => Array ( [lat] => -33.868362630292 [lng] => 151.19438836971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png [id] => 7eaf747a3f6dc078868cd65efc8d3bc62fff77d7 [name] => Biaggio Cafe [opening_hours] => Array ( [open_now] => [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 3036 [html_attributions] => Array ( [0] => Briggs Soria ) [photo_reference] => CmRaAAAAcPXEEuCbXQiIU-c0rFzFwHhpmRPEDkZ-7D6ep2gpkEG8Yyx6-aB6o4cp2LWce728a-Eb9izN9ju-TAe6sdURdcpX-UpJwMHR7Ynj1H4HYy7R4WUWtqxbN7hifOWqV6TzEhD0g0k5X-KCKBu9RdWpl-47GhQwPBWXcBL0LnMWEp9ItV7REPcmTQ [width] => 4048 ) ) [place_id] => ChIJIfBAsjeuEmsRdgu9Pl1Ps48 [price_level] => 1 [rating] => 3 [reference] => CmRSAAAAiI3HIH3xQoIx0spB-d1aye7c7W4L3s25RxdAHwwLVEvC5AovkxwscFsfi-1ZKAL9V-DAPda5d9FH_ywnl7Ll3vovPw5v6T08uNxVwJyMn9MPJFHWZN06dJT1f50vZ19qEhBfOVCaYhc8f9tk2UZLkgAOGhSSKLENUQ1taZOQWolQVsJ0V48bQA [scope] => GOOGLE [types] => Array ( [0] => cafe [1] => restaurant [2] => food [3] => store [4] => point_of_interest [5] => establishment ) [vicinity] => 48 Pirrama Road, Pyrmont ) [1] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8671288 [lng] => 151.195339 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866065569709 [lng] => 151.1960316 ) [southwest] => Array ( [lat] => -33.868763530292 [lng] => 151.1932612 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png [id] => 8bdf85385635caa3b9df55ec23d533ceb0f2b8c6 [name] => The Century [opening_hours] => Array ( [open_now] => 1 [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 1989 [html_attributions] => Array ( [0] => The Century ) [photo_reference] => CmRaAAAAbC2XEW4KdedbESdFb1Bgija2OV_9mR8t6DJqMZwmLjwXAtG5JgWYfzUi7nEx0ZKqhhsDxtPceC3MPKbYJIVh7VWT_CDvsN7hRtW2bdpxss87I9DbTn450Vd7dn0wOueWEhBGiONsANF-eOu1xwrVMXP4GhTqtBdrIP84yK19IXGwZVqazpEACQ [width] => 2048 ) ) [place_id] => ChIJ1-v38TauEmsRNrXszdcSywQ [price_level] => 2 [rating] => 3.5 [reference] => CmRRAAAAyJYatPQTSvqMbOt1gIhNxfc1Sy7fnXKDBaX0lYkx16_u2HEu8hyBH_XriKc1Aciq3Gx8FxQI_OkXxRTFh2xz8eJbV8lOA6gFoMxDbvV3nQN2yCAwwgwo60wTKavRnxcOEhBT6whUoikUKFrq4EXRcK4OGhRgUy8KHjA9IS8ckfED_CNkdScvvg [scope] => GOOGLE [types] => Array ( [0] => restaurant [1] => food [2] => point_of_interest [3] => establishment ) [vicinity] => The Star, 80 Pyrmont Street, Pyrmont ) [2] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8667062 [lng] => 151.1957844 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.865427419709 [lng] => 151.19697313029 ) [southwest] => Array ( [lat] => -33.868125380292 [lng] => 151.19427516971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png [id] => fd0f490655de01f49b604781f944340f6623d1b1 [name] => Biaggio Cafe [opening_hours] => Array ( [open_now] => [weekday_text] => Array ( ) ) [place_id] => ChIJA0NrtTeuEmsRkmB_OMsgGOw [reference] => CmRSAAAANgrbbAHDKaIJ1OR0BFJ6t2Jlk0fKKDN9-hnfaDtSU2Xirm_66SOV4Wpy_w7HL6WVv0JXy4vrUdWkLMVizwGS1XpjSqDs-M6WbzSyGr6C-gJdLCUwDMPf6RBpCfeo279REhApljm2GRCvCVzFqcZ0pkGqGhRttJFZd6bXU67b3i-YN-N7ucP2oA [scope] => GOOGLE [types] => Array ( [0] => cafe [1] => restaurant [2] => food [3] => point_of_interest [4] => establishment ) [vicinity] => 48 Pirrama Road, Pyrmont ) [3] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8667062 [lng] => 151.1957844 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.865357219708 [lng] => 151.19713338029 ) [southwest] => Array ( [lat] => -33.868055180291 [lng] => 151.19443541971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png [id] => 3ef986cd56bb3408bc1cf394f3dad9657c1d30f6 [name] => Doltone House - Darling Island Wharf [opening_hours] => Array ( [open_now] => [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 843 [html_attributions] => Array ( [0] => Doltone House - Darling Island Wharf ) [photo_reference] => CmRaAAAAkdwvY7mKPHP6aCqmXjNPithGEIBaytETIG8GtU70vtAsymKWMaYhw4-VW6_W3HKyH9ddeVjnRFWwmWFlZO2yNLZ8IPDGmPSbqqtA4TzwZL9XddUH1eBGg-BhlicwYUTYEhAnqcUR2JQcwa8O54DUD1_XGhRGg60_1NqarQ1UbtXiJzaPq5MtxA [width] => 1500 ) ) [place_id] => ChIJ5xQ7szeuEmsRs6Kj7YFZE9k [rating] => 4.2 [reference] => CmRSAAAAVzanf0yiJg-X4Y2F94Wj-SjxZLi7i4zwrXM-0mEkXgmzelK7Jus6fGWbKsqeGH8fSbzn8oNBGhyV5VvewU4AXFoUx5uMjrg6A5Ho8vccsfL0Dq5PMReQiXYUKYBDoXPNEhCmcU6_Vat2qDmjKaPtJIeYGhT2c8QasUnZCrUQpd_RFUO1_Jf6lQ [scope] => GOOGLE [types] => Array ( [0] => food [1] => point_of_interest [2] => establishment ) [vicinity] => Ground Floor, 48 Pirrama Rd, Pyrmont ) [4] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8667062 [lng] => 151.1957844 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.865357219708 [lng] => 151.19713338029 ) [southwest] => Array ( [lat] => -33.868055180291 [lng] => 151.19443541971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png [id] => 2de2d85c0465dafc9a75e0304859c5ceedb4a679 [name] => Signorelli Gastronomia [opening_hours] => Array ( [open_now] => [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 390 [html_attributions] => Array ( [0] => Signorelli Gastronomia ) [photo_reference] => CmRaAAAA5VEkXQg5NXRvvbjQ-sHCF1rusIPBRItqIXb7ctqawTC8_ZG4b0zPBGBLSA8ttYHp1D2DFFaqT0WtsXLT0tmSBXnJ47-Ei94jKjg5jCIpWt5dlweWMMyx5duOEBNB2biLEhBZqLCcaoSa2tVU6zZEWewGGhQ9JSj_mVqMDeizYtrcYVaCYVz6sQ [width] => 960 ) ) [place_id] => ChIJFcJDMzauEmsRy81NJCujoY8 [price_level] => 2 [rating] => 3.9 [reference] => CmRSAAAAJcg6WX2L8zTXoizEVUB5lyaKW22-sMoMsiOBaT8Oa4stB2PrbrObqZ2boBIIR8xxpyFIpf7q-HZuuDp3oXJ4Wa964l1koOq7jEjk7l_2wFHWdAsEF0ekpmvZV4rTC_nTEhAfwAlxYeqXvgmAL5vBlEjuGhSkxSkyhxCJOZo2gr4lUa7YU5NSww [scope] => GOOGLE [types] => Array ( [0] => food [1] => point_of_interest [2] => establishment ) [vicinity] => Ground Floor, 48 Pirrama Road, Pyrmont ) [5] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8664812 [lng] => 151.196061 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.865132219708 [lng] => 151.19740998029 ) [southwest] => Array ( [lat] => -33.867830180291 [lng] => 151.19471201971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png [id] => 91509349e192972859b886e62f11827e5bd0072f [name] => Esky Cafe [opening_hours] => Array ( [open_now] => [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 1536 [html_attributions] => Array ( [0] => Paul Toogood ) [photo_reference] => CmRaAAAAWHv_VoJ9mDttpdl8XXeM78_bDtutVfhNIwoz57Va1CEzOrxzN5XAtuu1OwzwU8NoH6prpkLzdALY0DhdKz4reJkejB_AV4NYrbFbztRRG5xcJGthqTHOzzdDXzBTkPk-EhDVmJVlHn-tap_okguRQ4eKGhQ78ZojGOFEKHxpPeJGTBaNa68yDg [width] => 2048 ) ) [place_id] => ChIJm3d7yTeuEmsRC2wYUB24q9I [rating] => 4.4 [reference] => CmRSAAAAOEtb2LjP1kZkWxsgcq5mGm6NWlAlwMOTXQnF3HFvwRN9-cIl0-RJU8-nT3Jp8dRlib9o0M2U0JFauLwjjOw0fqPadh2OBVqGde8LqbzVP4FUIlmKR-lKOCuC1U8Fyw1QEhB0T-m_Jh_h4ooBbl4YxQQOGhQC_bkk9d2Fyraihp5Q6-njdS1QEg [scope] => GOOGLE [types] => Array ( [0] => restaurant [1] => cafe [2] => food [3] => point_of_interest [4] => establishment ) [vicinity] => Workplace 6, 48 Pirrama Road, Pyrmont ) [6] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8677499 [lng] => 151.1956285 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866673069708 [lng] => 151.19631778029 ) [southwest] => Array ( [lat] => -33.869371030291 [lng] => 151.19361981971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png [id] => b21cfc23b85bda32717dc32bbb3b0d290e1692bf [name] => Kampung [opening_hours] => Array ( [open_now] => [weekday_text] => Array ( ) ) [place_id] => ChIJ1-v38TauEmsRkfBaAXNA5LI [rating] => 5 [reference] => CmRSAAAAh9Vuux8EylTbKIFyoWDxeWXxXHp8fsNM7Q97h8W9_TxvAo23lTjahLW0vGFAnlmHG3qIu4WXfo5XlgS42tQIsp5RiAiFiuqLtlXX1uRI4JxJrpi3oSxCp75fRlxGJsgrEhB2JLQK_7pdw-RhP6XDi12XGhTcmREuPiKty8znAmNvc1VPE2Unvg [scope] => GOOGLE [types] => Array ( [0] => restaurant [1] => meal_takeaway [2] => food [3] => point_of_interest [4] => establishment ) [vicinity] => 80 Pyrmont Street, Pyrmont ) [7] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8677499 [lng] => 151.1956285 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866673069708 [lng] => 151.19631778029 ) [southwest] => Array ( [lat] => -33.869371030291 [lng] => 151.19361981971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png [id] => f67efa5095d123ed2c1ce28e9297cc1e04624dc8 [name] => Lotus Pond [opening_hours] => Array ( [open_now] => 1 [weekday_text] => Array ( ) ) [place_id] => ChIJS_-h7TauEmsRdmDJ2CvXToY [rating] => 3.2 [reference] => CmRSAAAApnMGhFr1SKg2H4LpIDfJXqOAy2OUDSy_NaWA6ZOetI47fBG-Un_heBunbN2ozjyuhac5z7MHXOCwlGchZRpOJtOJupqfhOHGP93S2fvSTUt8nLUUo39WKBvpHUMrfCKmEhBuSr4HMwgKwUJkpjT5ffUnGhRQejzefZ3S3vCStqCxF8wAlBt1JQ [scope] => GOOGLE [types] => Array ( [0] => restaurant [1] => food [2] => point_of_interest [3] => establishment ) [vicinity] => 80 Pyrmont Street, Pyrmont ) [8] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8677499 [lng] => 151.1956285 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866673069708 [lng] => 151.19631778029 ) [southwest] => Array ( [lat] => -33.869371030291 [lng] => 151.19361981971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/bar-71.png [id] => 882abf952e4d0e6dd96294459abe076c90c127dd [name] => 24/7 Sports Bar [opening_hours] => Array ( [open_now] => 1 [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 638 [html_attributions] => Array ( [0] => 24/7 Sports Bar ) [photo_reference] => CmRaAAAAYsUMTqwU7RRG7gB1ft1yChuVhIvF4_TZ1NBRyyQdQs163bRXTh0X2ZJ_4AzjnNQztgZgOFxieDONaMGfqBlfQ3GWjgMFPZ3m-Ut-UsumAyDi2FQoo25vDBp58CA2kfdwEhDhNViiTe0vPZz2FgiFPphqGhTx1ZfQLwYRebnXV4I0q8yS1VF1Sg [width] => 960 ) ) [place_id] => ChIJ77Cd7TauEmsRBV42CMtSans [rating] => 3.4 [reference] => CmRRAAAANyqljC8u7sy67zaoKmvy1yZF9BJcoCO4UBwiC0F6WIPMkhZo84UbBiyR_bngCy8mziKacKR7hqOmH0C5YqJryYPNSpHqeTahBB84i7wnezFrFYklzHCjDTt_DhDFW96mEhBXRQVuWptLLxd9XRES6X3pGhQF3k1348GfUaIlUVmFiE06ERWP6w [scope] => GOOGLE [types] => Array ( [0] => bar [1] => restaurant [2] => food [3] => point_of_interest [4] => establishment ) [vicinity] => 80 Pyrmont Street, Pyrmont ) [9] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8677499 [lng] => 151.1956285 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866673069708 [lng] => 151.19631778029 ) [southwest] => Array ( [lat] => -33.869371030291 [lng] => 151.19361981971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png [id] => e188acf29a29bc46f1cf844c0bf78f8b1464bd6f [name] => BLACK Bar & Grill [opening_hours] => Array ( [open_now] => [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 2160 [html_attributions] => Array ( [0] => Chopinand Mysaucepan ) [photo_reference] => CmRaAAAAwz1MNHctvLtTgc7iFYmuDljnVuWyKnajNBh0z24Il7cTm7TAiJlAlnzvsgUp5JVULO8_oPFxW6DkjKLqN-MI0T703nJzFYGT79Yx36tJxuZaBAkZ5xIAis7T51pXYQ-wEhCupMDWqDDxv_3YsfJZnsQPGhQBp2vGHZWl6ECDGdoGt17Hz28PJg [width] => 3232 ) ) [place_id] => ChIJ9ZCzFzGuEmsR_EwB_qra-W4 [rating] => 4.1 [reference] => CmRRAAAAkRe25ds64LLF7mzx0Qdiy-d2kqa1WQzeNHH6_SsiDeZFdzjorMWenC7z4VRTw7BdNocoPzjqb6AjICSKQZCR6fW_xVHmsEUQNJseFAJ-6hDlgv2PpoBJ9pU5PnK221T3EhBJ3CwsMrtaSdukyZyDIpWvGhSeLY4vgz6nO6ZSW5bb0gcQsJxIOQ [scope] => GOOGLE [types] => Array ( [0] => restaurant [1] => bar [2] => food [3] => point_of_interest [4] => establishment ) [vicinity] => G, 80 Pyrmont Street, Pyrmont ) [10] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8677499 [lng] => 151.1956285 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866673069708 [lng] => 151.19631778029 ) [southwest] => Array ( [lat] => -33.869371030291 [lng] => 151.19361981971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png [id] => 8ed2a95d07a89918c0a1b846544a955b36820af3 [name] => Dergah Grill [opening_hours] => Array ( [open_now] => 1 [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 992 [html_attributions] => Array ( [0] => Dergah Grill ) [photo_reference] => CmRaAAAAjsOwnxHbfcRXNSsW7sVFA88bbK7XoeeI8etp5i3Rkmfvw2Nv2oOGbVoJzNZrzUwBrFpTRv_pU27OzahrLlCUFFLGz-_P2Z7Hn4jH0waC4urQg18yPbyE2qHFXRAh2sF_EhBK6Dk08w1Z44NTsgzbBVSMGhRLCr0qizP00LjX_T6jUh_986vnWA [width] => 1487 ) ) [place_id] => ChIJ1-v38TauEmsR7cWnOpSlMWU [rating] => 3 [reference] => CmRRAAAAz-ES7KMUShGEIn28_6aURVGtGr3kEJR9SzWKTgvXo9QDYSJAIVSe2WD02y4H9aJEv3IXJUTl1wH4iMxivWmNwNNJoSCotqKo7Dky9K4dpVSKwSLJvyGA65G1tuHzswMzEhAz3rOaYTJh9qr42kVmFeFdGhQwYDAjO2IYp6ypv5ot-QFZvZzJOA [scope] => GOOGLE [types] => Array ( [0] => meal_takeaway [1] => restaurant [2] => food [3] => point_of_interest [4] => establishment ) [vicinity] => 80 Pyrmont Street, Pyrmont ) [11] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8677499 [lng] => 151.1956285 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866673069708 [lng] => 151.19631778029 ) [southwest] => Array ( [lat] => -33.869371030291 [lng] => 151.19361981971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png [id] => 5895b175f4c2a8535294f5587fe089fc7d5135ea [name] => Pulse Express [opening_hours] => Array ( [open_now] => [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 992 [html_attributions] => Array ( [0] => Pulse Express ) [photo_reference] => CmRaAAAAg1N3QTCieHLyOJE0-UsbTQJJnawb4ZTuoB_-WPf9Wz4fOQbtJGvRWbUnppV_RgKaq5heQuEMBGg9kI0iXGxrDVPOEnQ3Zyh-iLbge3xpuZyQI0mjS6BlvyxRdNPvn3lVEhChKRAL5I5nHP_QU_a7aJjWGhTlwb7eywRH-kh7dx-spnPvP4bUXA [width] => 1618 ) ) [place_id] => ChIJ1-v38TauEmsRJxe1QBBHBGU [rating] => 2.6 [reference] => CmRRAAAALsYqD7jAxdAy8zpoDEWxfFD9PFNlJdN8o-krm3fupp77oxNnP-fJJqvplpFIxWTFA1pjVwFG0ozgeao2PhoL0VoKPDT6Gy6qi56NKyusNn6eDkfzLctO3iDB4Dx_inomEhAjhFof0CLWU1vFJ4VMOfvjGhSVYSHLz7kQml-Ne-VJVJuawxywRg [scope] => GOOGLE [types] => Array ( [0] => cafe [1] => restaurant [2] => food [3] => point_of_interest [4] => establishment ) [vicinity] => 80 Pyrmont Street, Pyrmont ) [12] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8678337 [lng] => 151.1958116 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866484719709 [lng] => 151.19716058029 ) [southwest] => Array ( [lat] => -33.869182680291 [lng] => 151.19446261971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png [id] => f138865c7ac72dcc2cbd0491710aa6203ebc595f [name] => Pizzaperta [opening_hours] => Array ( [open_now] => 1 [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 440 [html_attributions] => Array ( [0] => Pizzaperta ) [photo_reference] => CmRaAAAADCeKPVnBAGuxYOy03lNLG-7HuR5VQ6fBNFQmvJsCX52UBYB_wV4_450EskliO7fGWAwZ6mXhNLgonnqp0I9ixGFOeWzBuT2ChqPPoET9ofpKYiwNi9t4d7fl5NQTYyc5EhCxeM8QlMlbKP65Mku5ZAnDGhQR-zt6WCXpQXmhJ5gmXSdamamTWQ [width] => 700 ) ) [place_id] => ChIJA6-_qzeuEmsRiV9SAHIbzh4 [rating] => 3.4 [reference] => CmRRAAAAn-uUOFjUKSV18ENqqy65iOzr3Ea1H3ipK-9c3UX1IsTJPgb_-gd6d4BrsUIDcKZmwAnQZ8rtesI8jA0V46-9ovQh9RiGXmJsLrz3UyrKSxkhOuLXvFzyKu2z3Ja8QbGMEhDo-m7u5hyHfMmgPTcdKsWOGhRo5Fizn7eEefTbuOl9D8GfMAmX8A [scope] => GOOGLE [types] => Array ( [0] => restaurant [1] => food [2] => point_of_interest [3] => establishment ) [vicinity] => The Star Sydney, Harbourside Entry, Pirrama Rd, Pyrmont NSW 2009, Sydney ) [13] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.867931 [lng] => 151.1958456 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866582019709 [lng] => 151.19719458029 ) [southwest] => Array ( [lat] => -33.869279980292 [lng] => 151.19449661971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png [id] => 372b98902d4032b2708939131b8bc6bfbce9044c [name] => Chao Praya City [place_id] => ChIJ1-v38TauEmsRwCiun0VT1Kg [rating] => 5 [reference] => CmRSAAAA4HOsgFazyJOqUAEAfPyuQif3apB-IrYQ_UVZTyvwYLm18txQlrvMOWcaZp98ggKyWEoA0ea17TSlUNwHxJt3YLAOY_Aj6JSaOtRWxl3HdIoGwFGpVzVo37OyXen7d5nCEhDy43UD1zpBwgahbZGtW7nBGhRiLaMA4r9IsOXzLTe7-tm-ZzDNRA [scope] => GOOGLE [types] => Array ( [0] => restaurant [1] => food [2] => point_of_interest [3] => establishment ) [vicinity] => P1 Level G Star City Pirrama Road, Pyrmont ) [14] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8680279 [lng] => 151.195857 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866961369708 [lng] => 151.1965416 ) [southwest] => Array ( [lat] => -33.869659330291 [lng] => 151.1938032 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png [id] => 08c0f74375ca1956d8ae2919512171adcd8266d7 [name] => Balla [opening_hours] => Array ( [open_now] => [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 2882 [html_attributions] => Array ( [0] => Cynthia Chia ) [photo_reference] => CmRaAAAAQ3bhhMFaAoi-XSdDMIl1abzVHbm92X575sZ--9E828fgDgt1va8s8to_iCVMjruKD-E2vECITkXW_hwBP9iS4NE8fJSyEj2ZrK6eCP5HAiafZJT0DUsRkiWxVQ66Ud8mEhBDEgwL9JaYlPOaTIlT81MpGhTK_Bmrs14OfusC2d7vJ3SoRIUiKw [width] => 3843 ) ) [place_id] => ChIJ1-v38TauEmsRFBHuvMmwNyA [price_level] => 3 [rating] => 3.7 [reference] => CmRRAAAAxa4gkSU26kt1A9BMsaxySAdOysJGpreX08qD8FY-w40v6J36GYo1xIZJ9vy-T8zbjNLXr1PEXD458O72KpZr83kNlEuZIMYxQcodSdkAFtMJzbsaCCm2ldzYjeF-cDM1EhDLMSnhPjgnj33vJn13HV6GGhRNG8bcAGL_d9kMXxMMxfJjjGyffA [scope] => GOOGLE [types] => Array ( [0] => restaurant [1] => food [2] => point_of_interest [3] => establishment ) [vicinity] => The Star, G, 80 Pyrmont Street, Pyrmont ) [15] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8679022 [lng] => 151.1951276 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866727419708 [lng] => 151.19605433029 ) [southwest] => Array ( [lat] => -33.869425380291 [lng] => 151.19335636971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png [id] => 0761a21e23eda2598330d428413cb59431316a61 [name] => Lobby Lounge [opening_hours] => Array ( [open_now] => 1 [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 1632 [html_attributions] => Array ( [0] => Andrei Setiawan Yohannes ) [photo_reference] => CmRaAAAA5WtwRlnY-IB_agGsXdnruKoylr826EKf-vmptRBP68TXnQPuT5DPGByyl9VCMiC-_tt7PGY-qVVoXQnEue-fS9S2oWyyiUoXA_XTnriNyC0TasEwfyqMI2tHnfEn4r_5EhBZpIz5HXkqmqBK6HQhxQ4yGhRBLF-Xg9yILQPoB8KW4bT6xJn1Ag [width] => 920 ) ) [place_id] => ChIJ1-v38TauEmsRHbUt24abGq8 [rating] => 3.9 [reference] => CmRSAAAALvC6LDeHGtKa-1BfrK8GxGZJms5-W23jkxgNHUNfUAWST9IIfNzcqPKy7MMuaLztr5sIZpIiEs5YBhM4PtAccuBSdJSx5LaWK-E5ObIs_LZPCwtOikhG0NMtAf6D_hbpEhD2eT7U7sfSDnKg98_RWkd8GhQD3C5E8vTUxAO_p1GxO7w5oYtcCw [scope] => GOOGLE [types] => Array ( [0] => cafe [1] => food [2] => store [3] => point_of_interest [4] => establishment ) [vicinity] => 80 Pyrmont Street, Pyrmont ) [16] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8679658 [lng] => 151.1952022 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866350469709 [lng] => 151.19779985 ) [southwest] => Array ( [lat] => -33.869048430291 [lng] => 151.19280965 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/lodging-71.png [id] => 44fdc977984610cd873c790a7d850e8185ed0e5e [name] => The Star Sydney [opening_hours] => Array ( [open_now] => 1 [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 1978 [html_attributions] => Array ( [0] => The Star Sydney ) [photo_reference] => CmRaAAAAAKL4p8pfqn9KTX1IP4mC6wbRpfJB2R8wdaAT7y6fmdLL8-5XIdgggJp31eE9S0tJQbifbUqDCCjqH1YK74u2ezYck-6_m7MB7FG9fBCKcW_gv5Utyt5nHcC2IiW7GpfFEhCNnJ_cBhkAaSf9vcmYiQ0SGhROkORa05CJzj2d8kBLLRdg6q39OA [width] => 2048 ) ) [place_id] => ChIJq6qq6jauEmsRJAf7FjrKnXI [rating] => 4 [reference] => CmRRAAAAP2aFIXavvX5VXtZkh_BsYrNfKlzYMJS-LeoZWPUzOaY96nVOpuANmGxyBnS-A25653IYjCOBOqGpNTLYdUALm75rICJmk9BkV29IHuS11I-gKFgviOkQnjC-QDMU6NMOEhDvOnNCgVPkvBeXh5KWnKDGGhT5qVnYHznM3i7qGh_bvJ3Vv2uGBA [scope] => GOOGLE [types] => Array ( [0] => casino [1] => spa [2] => lodging [3] => restaurant [4] => food [5] => point_of_interest [6] => establishment ) [vicinity] => 80 Pyrmont Street, Pyrmont ) [17] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.867978 [lng] => 151.195171 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866802919708 [lng] => 151.19609848029 ) [southwest] => Array ( [lat] => -33.869500880291 [lng] => 151.19340051971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png [id] => 7d4beb38f8a160ca1758002b846afbf713cfe432 [name] => Vantage, The Star [opening_hours] => Array ( [open_now] => 1 [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 3024 [html_attributions] => Array ( [0] => Neek Woman ) [photo_reference] => CmRaAAAA09pV6Ak9le5QPb1Djz9eCeBAK1MaBhTjF_XWBw-OdOwDP3xY-vHGjnC7RK8lRUd6qZ6OFccb3woPGGZxL7C4v-MVThoMqRfbYVaET-GQoN6OgFFFXtluaR-fYSj2k_IFEhCPSwX1E1O_Bb2Pz49_GYvUGhSr70Gmld6MWqoTdMkgm00mkl4S4w [width] => 3024 ) ) [place_id] => ChIJ2fIN-TauEmsRj8cqw1d-YI0 [rating] => 4 [reference] => CmRSAAAALWJKfuthS5deCrLk_fBqSMVa_cXG5uBPDFrF5ifHvitgFsdOfhkzgW4-58x6qfRdm2m5kJE950tJ-z7w6V3K7eDqF8P00O-gJIfWQg6agRh3L-5WZdKvgqfMZd6JLmfOEhCNSI1TqwbYLWWT-xTs2mLHGhTeOfVM-lR9v9jMlJvPa7pqk_aucg [scope] => GOOGLE [types] => Array ( [0] => restaurant [1] => casino [2] => food [3] => point_of_interest [4] => establishment ) [vicinity] => 80 Pyrmont Street, Pyrmont ) [18] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8669724 [lng] => 151.1944552 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.865714269709 [lng] => 151.19589088029 ) [southwest] => Array ( [lat] => -33.868412230291 [lng] => 151.19319291971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png [id] => dd8ce7e0a6f502325a4a5f45abfa2e1dc3784e07 [name] => Ezy mart pyrmont [opening_hours] => Array ( [open_now] => 1 [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 899 [html_attributions] => Array ( [0] => Ezy mart pyrmont ) [photo_reference] => CmRZAAAAxwL2stPwv920nCiDOd3qm8bUIZZ3DAolWumYyMOpjKSjo6kYnO_YDoHzWFICKERPnIPucrIbdOrP8oY06LKBkaVLWX9UwPiMugbCcpAt5bZjT2PDhnwT8S4fleleT4pFEhC47nMqVf_XsAOtVVK0quhkGhR4_lUCOwYmhQib9FhrxKpdTHWA8A [width] => 900 ) ) [place_id] => ChIJV37FXDauEmsRL6aHZGJ7Q04 [reference] => CmRRAAAAtFUKLoOXju5F3e6L_dVsuKxI2jeXsraJOdU9Ilzg2U66aJb-TSB9OacxmgovqGQhb6I9nUOIQnUmDdgJsPEaziy9xf7ztkNKMj1VemSjzLeFnF95SB0bUAyu0L25Fj-4EhC1FPac5lMJy-FmkPWdm5EyGhSlBzIOA3oqBAiszlxbCL7BTy4IRw [scope] => GOOGLE [types] => Array ( [0] => convenience_store [1] => food [2] => store [3] => point_of_interest [4] => establishment ) [vicinity] => 1/2 Jones Bay Road, Pyrmont ) [19] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.868028 [lng] => 151.19521 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866679019708 [lng] => 151.19655898029 ) [southwest] => Array ( [lat] => -33.869376980291 [lng] => 151.19386101971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png [id] => ba9494fbebdb7e40f9a646aab93379212763195c [name] => Fat Noodle [opening_hours] => Array ( [open_now] => 1 [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 469 [html_attributions] => Array ( [0] => Fat Noodle ) [photo_reference] => CmRaAAAA0zMsUGM3z6dLIJmGiOcA39rcUNhUSJJlNQAAqqEbQzOiWhTeYAAdfJsjB1aBIHtDysC3CJwyHxKXmpXweRBv2G0-60FZVdawXVRiTyA2KTnG-2qgAm6h9k2FfDIzhsUIEhB2XzrAKGRuxOEnCowpQ8i1GhT-FQbOYvsdFgdhR6QdRwBsOKjIrA [width] => 750 ) ) [place_id] => ChIJ1-v38TauEmsROWultgmCYW0 [price_level] => 1 [rating] => 3.2 [reference] => CmRRAAAAlCa7aYxV6SgfJZII_AqswB88Oo2q0--NWOibat1k2OuiuBenPkZ3IKbTB-d8iQg2AupBZxtaY4DuS27eE1YYNrlZ4kRk2G7IaY2j9uP73zpZNT7KS8ASw2ibmiCM9Y2hEhC2fpgHMn6HxPpTpDS-Sts2GhQB3NhqtuZKtouPwU4j0jXFlz0NzQ [scope] => GOOGLE [types] => Array ( [0] => restaurant [1] => food [2] => point_of_interest [3] => establishment ) [vicinity] => The Star, 80 Pyrmont Street, Pyrmont ) ) [status] => OK )
Checking at the structure of the json got from the call, to access the value you want to need to do like this.
$string = file_get_contents("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&rankby=distance&types=food&key=AIzaSyCpOG91fkAzPTZiFq7G9HbLfbmVyW8Wgyc");
$json_a = json_decode($string, true);
print_r($json_a['results'][0]['name']));
Results is an array, and you want to access to its first element ([0]). From that first element you want to get the property name. And all the properties on the element are set inside an array, so you need to access it by the property name.
You were using the object operator (->) to access elements from an array, that's why it didn't work.
echo print_r(....); is in a way redundant. print_r it self prints result to output in similar way as echo and returns TRUE which is what you are printing.
From documentation:
mixed print_r ( mixed $expression [, bool $return = false ] )
When the return parameter is TRUE, this function will return a string. Otherwise, the return value is TRUE.
Either do not use echo or set the return param to TRUE.
echo print_r($array, true);

get country, region and city name from longitude and latitude (PHP)

I would like to get country, pincode,state and city name from longitude and latitude over Google maps API - with PHP. but sometimes it works correct, but sometime it gets wrong information and many times it returns null values because the result comes is dynamic not fixed.
so anybody suggest me how to make this dynamic and check inside is country city pincode and state are available in any of the array index from result ....
below is my code...
$fromlat = $this->request->data['Order']['from_lat'];
$fromlong = $this->request->data['Order']['from_long'];
$geocode = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng='.$fromlat.','.$fromlong.'&sensor=false');
$json = json_decode($geocode);
//print_r($geocode);
$country = $json->{'results'}[1]->{'address_components'}[3]->{'long_name'};
$state = $json->{'results'}[1]->{'address_components'}[2]->{'long_name'};
$city = $json->{'results'}[1]->{'address_components'}[1]->{'long_name'};
$locality = $json->{'results'}[1]->{'address_components'}[0]->{'long_name'};
$pincode = $json->{'results'}[1]->{'address_components'}[4]->{'long_name'};
echo $locality."</br>".$country."</br>".$state."</br>".$city."</br>".$pincode;
i need to check types in address components like below in result forloop and if available then display result.
types = country = country name;
types = administrative_area_level_1 = state name ;
types = administrative_area_level_2 = city name ;
types = postal_code = pincode ;
below is my result array...
stdClass Object
(
[results] => Array
(
[0] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Kurkheda - Wadsa - Bramhapuri - Nagpur Highway
[short_name] => MH MSH 9
[types] => Array
(
[0] => route
)
)
[1] => stdClass Object
(
[long_name] => Nagpur
[short_name] => Nagpur
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[2] => stdClass Object
(
[long_name] => Maharashtra
[short_name] => MH
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[3] => stdClass Object
(
[long_name] => India
[short_name] => IN
[types] => Array
(
[0] => country
[1] => political
)
)
[4] => stdClass Object
(
[long_name] => 441204
[short_name] => 441204
[types] => Array
(
[0] => postal_code
)
)
)
[formatted_address] => Kurkheda - Wadsa - Bramhapuri - Nagpur Hwy, Maharashtra 441204, India
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 21.0633475
[lng] => 79.1652473
)
[southwest] => stdClass Object
(
[lat] => 21.0599518
[lng] => 79.1646723
)
)
[location] => stdClass Object
(
[lat] => 21.0616013
[lng] => 79.1647236
)
[location_type] => GEOMETRIC_CENTER
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 21.0633475
[lng] => 79.166308780291
)
[southwest] => stdClass Object
(
[lat] => 21.0599518
[lng] => 79.163610819708
)
)
)
[place_id] => ChIJj5wNX6O51DsRz53GLG9aZRM
[types] => Array
(
[0] => route
)
)
[1] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Kalamana
[short_name] => Kalamana
[types] => Array
(
[0] => locality
[1] => political
)
)
[1] => stdClass Object
(
[long_name] => Nagpur
[short_name] => Nagpur
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[2] => stdClass Object
(
[long_name] => Maharashtra
[short_name] => MH
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[3] => stdClass Object
(
[long_name] => India
[short_name] => IN
[types] => Array
(
[0] => country
[1] => political
)
)
[4] => stdClass Object
(
[long_name] => 441204
[short_name] => 441204
[types] => Array
(
[0] => postal_code
)
)
)
[formatted_address] => Kalamana, Maharashtra 441204, India
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 21.0628156
[lng] => 79.164412
)
[southwest] => stdClass Object
(
[lat] => 21.0589309
[lng] => 79.1617083
)
)
[location] => stdClass Object
(
[lat] => 21.0605174
[lng] => 79.1629662
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 21.0628156
[lng] => 79.164412
)
[southwest] => stdClass Object
(
[lat] => 21.0589309
[lng] => 79.1617083
)
)
)
[place_id] => ChIJ2yhznKO51DsRGZWqNrtKBD4
[types] => Array
(
[0] => locality
[1] => political
)
)
[2] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => 441204
[short_name] => 441204
[types] => Array
(
[0] => postal_code
)
)
[1] => stdClass Object
(
[long_name] => Nagpur
[short_name] => Nagpur
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[2] => stdClass Object
(
[long_name] => Maharashtra
[short_name] => MH
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[3] => stdClass Object
(
[long_name] => India
[short_name] => IN
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Maharashtra 441204, India
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 21.139709
[lng] => 79.3452018
)
[southwest] => stdClass Object
(
[lat] => 20.8714046
[lng] => 79.0962536
)
)
[location] => stdClass Object
(
[lat] => 20.9903768
[lng] => 79.2451149
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 21.139709
[lng] => 79.3452018
)
[southwest] => stdClass Object
(
[lat] => 20.8714046
[lng] => 79.0962536
)
)
)
[place_id] => ChIJKTHfbpOw1DsRGwYyMATAfU4
[types] => Array
(
[0] => postal_code
)
)
[3] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Nagpur
[short_name] => Nagpur
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[1] => stdClass Object
(
[long_name] => Maharashtra
[short_name] => MH
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[2] => stdClass Object
(
[long_name] => India
[short_name] => IN
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Nagpur, Maharashtra, India
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 21.713785
[lng] => 79.65179
)
[southwest] => stdClass Object
(
[lat] => 20.5841799
[lng] => 78.2515599
)
)
[location] => stdClass Object
(
[lat] => 21.3170198
[lng] => 79.1999949
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 21.713785
[lng] => 79.65179
)
[southwest] => stdClass Object
(
[lat] => 20.5841799
[lng] => 78.2515599
)
)
)
[place_id] => ChIJ691n-cTA1DsRh3HGEikxZ2E
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[4] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Maharashtra
[short_name] => MH
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[1] => stdClass Object
(
[long_name] => India
[short_name] => IN
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Maharashtra, India
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 22.027909
[lng] => 80.890924
)
[southwest] => stdClass Object
(
[lat] => 15.602412
[lng] => 72.6593629
)
)
[location] => stdClass Object
(
[lat] => 19.7514798
[lng] => 75.7138884
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 22.027909
[lng] => 80.890924
)
[southwest] => stdClass Object
(
[lat] => 15.602412
[lng] => 72.6593629
)
)
)
[place_id] => ChIJ-dacnB7EzzsRtk_gS5IiLxs
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[5] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => India
[short_name] => IN
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => India
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 35.5087008
[lng] => 97.395561
)
[southwest] => stdClass Object
(
[lat] => 6.7535159
[lng] => 68.1623859
)
)
[location] => stdClass Object
(
[lat] => 20.593684
[lng] => 78.96288
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 35.5087008
[lng] => 97.395561
)
[southwest] => stdClass Object
(
[lat] => 6.7535159
[lng] => 68.1633666
)
)
)
[place_id] => ChIJkbeSa_BfYzARphNChaFPjNc
[types] => Array
(
[0] => country
[1] => political
)
)
)
[status] => OK
)
Proper way to extract values of address components is done as following :
if (results[0]) {
$("input[name=lat]").val(results[0].geometry.location.lat());
$("input[name=lng]").val(results[0].geometry.location.lng());
var addressArr = [];
$.each(results[0].address_components, function (key, val) {
if (!$.inArray("country", results[0].address_components[key].types)) {
addressArr['country'] = results[0].address_components[key].long_name;
}
if (!$.inArray("administrative_area_level_1", results[0].address_components[key].types)) {
addressArr['state'] = results[0].address_components[key].long_name;
}
if (!$.inArray("administrative_area_level_2", results[0].address_components[key].types)) {
addressArr['city'] = results[0].address_components[key].long_name;
}
});
console.log(addressArr);
}
}
I have some similar code that is known to work. Basically it can return a results array if there are more than one results possible or just an object if only one result is found. My use-case is slightly different, but hopefully it helps. Try this:
$ch = curl_init();
$geoCodeUrl = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . $zip . ',+United+States&key=' . $googleApiKey;
curl_setopt($ch,CURLOPT_URL, $geoCodeUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$result = curl_exec($ch);
curl_close($ch);
$GeoCode = json_decode($result);
$GeoLocator = new \stdClass();
if( isset($GeoCode->results[0]) ){
$GeoLocator->lat = $GeoCode->results[0]->geometry->location->lat;
$GeoLocator->lon = $GeoCode->results[0]->geometry->location->lng;
foreach($GeoCode->results[0]->address_components as $comp){
if( in_array('locality',$comp->types) ){
$GeoLocator->city = trim($comp->short_name);
}
else if( in_array('administrative_area_level_1',$comp->types) ){
$GeoLocator->state = strtoupper(trim(preg_replace('/\d/', '', $comp->short_name)));
}
}
}
else if( isset($GeoCode->results) && isset($GeoCode->results->geometry) ){
$GeoLocator->lat = $GeoCode->results->geometry->location->lat;
$GeoLocator->lon = $GeoCode->results->geometry->location->lng;
foreach($GeoCode->results->address_components as $comp){
if( in_array('locality',$comp->types) ){
$GeoLocator->city = trim($comp->short_name);
}
else if( in_array('administrative_area_level_1',$comp->types) ){
$GeoLocator->state = strtoupper(trim(preg_replace('/\d/', '', $comp->short_name)));
}
}
}

Getting the distance between two locations using Google Maps Api (JSON)

I want to get the distance between two locations using the Google API however this is the first time I hear about JSON and actually see its format. The variable $json_a should decode the JSON but when I try to get the distance I get "Notice: Undefined index: legs in C:\xampp\htdocs\app2\test.php on line 24". I added a link so you can see how the JSON structure looks like. I'll also want these locations ordered by distance and if you can provide any tips about how I could do that it would be great. Thanks! :)
PS: I know MySQL is depreciated, I'll look into MySQLi or PDO ASAP.
//$origin = $_COOKIE['origin'];
//$id=$_GET['id'];
$origin = "Bucuresti+Avrig+30";
$id = 3;
$query = ("SELECT * FROM locations WHERE category_id='$id'");
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result))
{
$address = $row['Judet'] . '+' . $row['Localitate'] . '+' . $row['Strada'] . '+' . $row['Numar'];
$address = str_replace(' ', '+', $address);
$fullurl = 'http://maps.googleapis.com/maps/api/directions/json?origin=' . $origin . '&destination=' . $address . '&alternatives=true&sensor=true';
$json_a = json_decode((file_get_contents($fullurl)),true);
echo $row['name'];
echo "<span class='badge'>" . $json_a['legs']['routes']['distance']['text'] . "</span></button>";
}
http://maps.googleapis.com/maps/api/directions/json?origin=Comanesti+Dimitrie+Ghica+13&destination=Brasov+1+Mai&alternatives=true&sensor=true
print_r shows me this:
Array ( [routes] => Array ( ) [status] => NOT_FOUND ) Array ( [routes] => Array ( [0] => Array ( [bounds] => Array ( [northeast] => Array ( [lat] => 44.4521487 [lng] => 26.1311305 ) [southwest] => Array ( [lat] => 44.4451446 [lng] => 26.1251047 ) ) [copyrights] => Map data ©2015 Google [legs] => Array ( [0] => Array ( [distance] => Array ( [text] => 1.2 km [value] => 1168 ) [duration] => Array ( [text] => 2 mins [value] => 137 ) [end_address] => Șoseaua Colentina 2, București, Romania [end_location] => Array ( [lat] => 44.4521487 [lng] => 26.1267169 ) [start_address] => Strada Avrig 30, București, Romania [start_location] => Array ( [lat] => 44.4451446 [lng] => 26.1311305 ) [steps] => Array ( [0] => Array ( [distance] => Array ( [text] => 0.4 km [value] => 397 ) [duration] => Array ( [text] => 1 min [value] => 42 ) [end_location] => Array ( [lat] => 44.4479077 [lng] => 26.1279682 ) [html_instructions] => Head northwest on Strada Avrig toward Aleea Avrig [polyline] => Array ( [points] => cugnGqvn~Ce#h#w#`AEDiArA_BnBm#r#QRsA~AeAlAaAjA ) [start_location] => Array ( [lat] => 44.4451446 [lng] => 26.1311305 ) [travel_mode] => DRIVING ) [1] => Array ( [distance] => Array ( [text] => 0.2 km [value] => 158 ) [duration] => Array ( [text] => 1 min [value] => 22 ) [end_location] => Array ( [lat] => 44.447097 [lng] => 26.1263385 ) [html_instructions] => Turn left onto Strada Chiristigiilor [maneuver] => turn-left [polyline] => Array ( [points] => mfhnGybn~CJTj#rAZv#Rh#f#nAPh# ) [start_location] => Array ( [lat] => 44.4479077 [lng] => 26.1279682 ) [travel_mode] => DRIVING ) [2] => Array ( [distance] => Array ( [text] => 0.3 km [value] => 282 ) [duration] => Array ( [text] => 1 min [value] => 32 ) [end_location] => Array ( [lat] => 44.4494197 [lng] => 26.1251047 ) [html_instructions] => Turn right onto Șoseaua Mihai Bravu [maneuver] => turn-right [polyline] => Array ( [points] => kahnGsxm~CiA#mAX[FGBMDWHEBc#Pa#T]TUPEBgBfB ) [start_location] => Array ( [lat] => 44.447097 [lng] => 26.1263385 ) [travel_mode] => DRIVING ) [3] => Array ( [distance] => Array ( [text] => 0.3 km [value] => 331 ) [duration] => Array ( [text] => 1 min [value] => 41 ) [end_location] => Array ( [lat] => 44.4521487 [lng] => 26.1267169 ) [html_instructions] => Turn right onto Șoseaua Colentina/DN2/E60/E85
Destination will be on the right
[maneuver] => turn-right [polyline] => Array ( [points] => {ohnG{pm~CYE[IoBk#oFeDYWYQq#[e#[ ) [start_location] => Array ( [lat] => 44.4494197 [lng] => 26.1251047 ) [travel_mode] => DRIVING ) ) [via_waypoint] => Array ( ) ) ) [overview_polyline] => Array ( [points] => cugnGqvn~CaL|MgCxCv#hBn#`Bx#xBiA#mAXc#JoAd#{A`AgBfBYEkCu#iG}DkAm#e#[ ) [summary] => Strada Avrig and Șoseaua Colentina/DN2/E60/E85 [warnings] => Array ( ) [waypoint_order] => Array ( ) ) [1] => Array ( [bounds] => Array ( [northeast] => Array ( [lat] => 44.4521487 [lng] => 26.131243 ) [southwest] => Array ( [lat] => 44.4438465 [lng] => 26.1251047 ) ) [copyrights] => Map data ©2015 Google [legs] => Array ( [0] => Array ( [distance] => Array ( [text] => 1.3 km [value] => 1282 ) [duration] => Array ( [text] => 3 mins [value] => 153 ) [end_address] => Șoseaua Colentina 2, București, Romania [end_location] => Array ( [lat] => 44.4521487 [lng] => 26.1267169 ) [start_address] => Strada Avrig 30, București, Romania [start_location] => Array ( [lat] => 44.4451446 [lng] => 26.1311305 ) [steps] => Array ( [0] => Array ( [distance] => Array ( [text] => 9 m [value] => 9 ) [duration] => Array ( [text] => 1 min [value] => 1 ) [end_location] => Array ( [lat] => 44.4450833 [lng] => 26.1312017 ) [html_instructions] => Head southeast on Strada Avrig [polyline] => Array ( [points] => cugnGqvn~CJM ) [start_location] => Array ( [lat] => 44.4451446 [lng] => 26.1311305 ) [travel_mode] => DRIVING ) [1] => Array ( [distance] => Array ( [text] => 0.2 km [value] => 209 ) [duration] => Array ( [text] => 1 min [value] => 45 ) [end_location] => Array ( [lat] => 44.4439255 [lng] => 26.1293794 ) [html_instructions] => Take the 1st right toward Șoseaua Mihai Bravu [polyline] => Array ( [points] => wtgnG_wn~CHCFAFAFAD#F#D#D#DBFH~#jCHRRj#BHZ`AJVPn# ) [start_location] => Array ( [lat] => 44.4450833 [lng] => 26.1312017 ) [travel_mode] => DRIVING ) [2] => Array ( [distance] => Array ( [text] => 0.7 km [value] => 733 ) [duration] => Array ( [text] => 1 min [value] => 66 ) [end_location] => Array ( [lat] => 44.4494197 [lng] => 26.1251047 ) [html_instructions] => Turn right onto Șoseaua Mihai Bravu [maneuver] => turn-right [polyline] => Array ( [points] => qmgnGskn~CNf#KPGL_#n#]j#m#|#e#h#[Za#b#eBrAqBxAIFk#Z_#R{#X{#RiA#mAX[FGBMDWHEBc#Pa#T]TUPEBgBfB ) [start_location] => Array ( [lat] => 44.4439255 [lng] => 26.1293794 ) [travel_mode] => DRIVING ) [3] => Array ( [distance] => Array ( [text] => 0.3 km [value] => 331 ) [duration] => Array ( [text] => 1 min [value] => 41 ) [end_location] => Array ( [lat] => 44.4521487 [lng] => 26.1267169 ) [html_instructions] => Turn right onto Șoseaua Colentina/DN2/E60/E85
Destination will be on the right
[maneuver] => turn-right [polyline] => Array ( [points] => {ohnG{pm~CYE[IoBk#oFeDYWYQq#[e#[ ) [start_location] => Array ( [lat] => 44.4494197 [lng] => 26.1251047 ) [travel_mode] => DRIVING ) ) [via_waypoint] => Array ( [0] => Array ( [location] => Array ( [lat] => 44.4445541 [lng] => 26.130791 ) [step_index] => 1 [step_interpolation] => 0.36662061075431 ) ) ) ) [overview_polyline] => Array ( [points] => cugnGqvn~CTQNCZBJDFH~#jC\~#^jA\fANf#KPg#|#kAhBaAdAa#b#eBrA{B`BkAn#wBl#iA#mAXc#Je#Ni#T_Aj#[TgBfBYEkCu#iG}DkAm#e#[ ) [summary] => Șoseaua Mihai Bravu and Șoseaua Colentina/DN2/E60/E85 [warnings] => Array ( ) [waypoint_order] => Array ( ) ) ) [status] => OK )
For finding the first value of routes, you can use below code :-
$json_a['routes'][0]['legs'][0]['distance']['text']
If you want to show all values then use foreach.
For sorting on values
function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
$a = array(3, 2, 5, 6, 1);
usort($a, "cmp");
foreach ($a as $key => $value) {
echo "$key: $value\n";
}

Php foreach remove duplicate using value [duplicate]

This question already has answers here:
How to remove duplicate values from a multi-dimensional array in PHP
(18 answers)
Closed 8 years ago.
I have an array like this
Array
(
[0] => Array
(
[idvox_business] => 5
[logo] => 1fJCb1_walmart_logo.jpg
[name] => Walmart
[icon] => http://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png
[lat] => 32.931393
[lng] => -96.840333
[miles] => 1
[place_id] => ChIJATuR8y0nTIYRQuI_GuSYqNs
)
[1] => Array
(
[idvox_business] => 41
[logo] => tqkuHo_bestbuy.png
[name] => Best Buy
[icon] => http://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png
[lat] => 32.9274
[lng] => -96.837284
[miles] => 1
[place_id] => ChIJVVVVVSknTIYReq3C1jDTAeM
)
[2] => Array
(
[idvox_business] => 41
[logo] => tqkuHo_bestbuy.png
[name] => Best Buy
[icon] => http://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png
[lat] => 32.927357
[lng] => -96.837275
[miles] => 1
[place_id] => ChIJs5gXoC0nTIYR-TFzaCpxVvE
)
[3] => Array
(
[idvox_business] => 16
[logo] => C25fIa_macys_logo.jpg
[name] => Macy's
[icon] => http://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png
[lat] => 32.92978
[lng] => -96.81953
[miles] => 2
[place_id] => ChIJWb8Ag9sgTIYRiPwoJIqw2rc
)
[4] => Array
(
[idvox_business] => 41
[logo] => tqkuHo_bestbuy.png
[name] => Best Buy
[icon] => http://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png
[lat] => 32.956207
[lng] => -96.815811
[miles] => 3
[place_id] => ChIJnVzmzT0hTIYRsWuHSl2p1Ig
)
)
I want to remove duplicates from this array but i need to omit only last unique array output need like this
Array
(
[0] => Array
(
[idvox_business] => 5
[logo] => 1fJCb1_walmart_logo.jpg
[name] => Walmart
[icon] => http://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png
[lat] => 32.931393
[lng] => -96.840333
[miles] => 1
[place_id] => ChIJATuR8y0nTIYRQuI_GuSYqNs
)
[1] => Array
(
[idvox_business] => 41
[logo] => tqkuHo_bestbuy.png
[name] => Best Buy
[icon] => http://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png
[lat] => 32.9274
[lng] => -96.837284
[miles] => 1
[place_id] => ChIJVVVVVSknTIYReq3C1jDTAeM
)
[3] => Array
(
[idvox_business] => 16
[logo] => C25fIa_macys_logo.jpg
[name] => Macy's
[icon] => http://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png
[lat] => 32.92978
[lng] => -96.81953
[miles] => 2
[place_id] => ChIJWb8Ag9sgTIYRiPwoJIqw2rc
)
)
Since the sub-arrays are not identical then the serialize solution will not work. This assumes removing based on identical idvox_business:
$ids = array();
foreach($array as $key => $vals) {
if(in_array($vals['idvox_business'], $ids)) {
unset($array[$key]);
} else {
$ids[] = $vals['idvox_business'];
}
}
Or possibly:
foreach(array_column($array, 'idvox_business') as $key => $id) {
$result[$key] = $array[$key];
}

Postcode of visitor location PHP

On my page I have a number of registered users, each user has a postcode stored in their row in my table.
I'm wondering if its possible and if so how to get the postcode of my website visitors and then show them the nearest users going of the data in my table?
You can use HTML 5 Geolocation to ask them to provide their location to you. Then you could lookup what the nearest postal code is based on their longitude/latitude.
You can get free postal/zip data with long/lat for North America here:
http://geocoder.ca/?freedata=1
If you can determine the coordinates of the visiting user in an accurate way you could use the google reverse geocoding API to translate the coordinates to an address and use the zipcode from that address.
Determining accurate coordinates of a visitor could prove difficult though. You'll have to rely on a GeoLocation api like http://www.maxmind.com/app/php or do an implementation of the HTML5 GeoLocation API.
You need to populate latitude/longitude for each record, then use Google Maps to find out nearest users, I believe following links will help you out.
Get latitude and longitude based on location name with Google Autocomplete API
Google Maps Api v3 - find nearest markers
Interesting, building on what others have said, perhaps this example will help ;)
<?php
//example a google IP
$user_info = get_ip_info('74.125.224.72');
$google_info = get_google_data($user_info['latitude'],$user_info['longitude']);
$user = array_merge($user_info,$google_info);
//See below for result
print_r($user);
function get_google_data($long,$lat){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://maps.googleapis.com/maps/api/geocode/json?latlng='.$long.','.$lat.'&sensor=true');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);
return json_decode($data,true);
}
function get_ip_info($ip = NULL){
if(empty($ip)) $ip = $_SERVER['REMOTE_ADDR'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://www.ipaddresslocation.org/ip-address-locator.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS,array('ip'=>$ip));
$data = curl_exec($ch);
curl_close($ch);
preg_match_all('/<i>([a-z\s]+)\:<\/i>\s+<b>(.*)<\/b>/im',$data,$matches,PREG_SET_ORDER);
if(count($matches)==0)return false;
$return = array();
$labels = array(
'Hostname' => 'host',
'IP Country' => 'country',
'IP Country Code' => 'country_code',
'IP Continent' => 'continent',
'IP Region' => 'region',
'IP Latitude' => 'latitude',
'IP Longitude' => 'longitude',
'Organization' => 'organization',
'ISP Provider' => 'isp');
foreach($matches as $info){
if(isset($info[2]) && !is_null($labels[$info[1]])){
$return[$labels[$info[1]]]=$info[2];
}
}
return (count($return))?$return:false;
}
/**
* Result:
*
*
* Array
(
[host] => nuq04s07-in-f8.1e100.net
[country] => United States
[country_code] => USA
[continent] => North America
[region] => California
[latitude] => 37.4192
[longitude] => -122.0574
[organization] => Google
[isp] => Google
[results] => Array
(
[0] => Array
(
[address_components] => Array
(
[0] => Array
(
[long_name] => Sevryns Rd
[short_name] => Sevryns Rd
[types] => Array
(
[0] => route
)
)
[1] => Array
(
[long_name] => Mountain View
[short_name] => Mountain View
[types] => Array
(
[0] => locality
[1] => political
)
)
[2] => Array
(
[long_name] => Santa Clara
[short_name] => Santa Clara
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[3] => Array
(
[long_name] => California
[short_name] => CA
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[4] => Array
(
[long_name] => United States
[short_name] => US
[types] => Array
(
[0] => country
[1] => political
)
)
[5] => Array
(
[long_name] => 94043
[short_name] => 94043
[types] => Array
(
[0] => postal_code
)
)
)
[formatted_address] => Sevryns Rd, Mountain View, CA 94043, USA
[geometry] => Array
(
[bounds] => Array
(
[northeast] => Array
(
[lat] => 37.4197948
[lng] => -122.0577433
)
[southwest] => Array
(
[lat] => 37.4175854
[lng] => -122.0590077
)
)
[location] => Array
(
[lat] => 37.4187254
[lng] => -122.0583059
)
[location_type] => APPROXIMATE
[viewport] => Array
(
[northeast] => Array
(
[lat] => 37.420039080292
[lng] => -122.05702651971
)
[southwest] => Array
(
[lat] => 37.417341119709
[lng] => -122.05972448029
)
)
)
[types] => Array
(
[0] => route
)
)
[1] => Array
(
[address_components] => Array
(
[0] => Array
(
[long_name] => Moffett Federal Airfield
[short_name] => Moffett Federal Airfield
[types] => Array
(
[0] => establishment
)
)
[1] => Array
(
[long_name] => Cummins Ave
[short_name] => Cummins Ave
[types] => Array
(
[0] => route
)
)
[2] => Array
(
[long_name] => Mountain View
[short_name] => Mountain View
[types] => Array
(
[0] => locality
[1] => political
)
)
[3] => Array
(
[long_name] => Santa Clara
[short_name] => Santa Clara
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[4] => Array
(
[long_name] => California
[short_name] => CA
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[5] => Array
(
[long_name] => United States
[short_name] => US
[types] => Array
(
[0] => country
[1] => political
)
)
[6] => Array
(
[long_name] => 94043
[short_name] => 94043
[types] => Array
(
[0] => postal_code
)
)
)
[formatted_address] => Moffett Federal Airfield (NUQ), Cummins Ave, Mountain View, CA 94043, USA
[geometry] => Array
(
[bounds] => Array
(
[northeast] => Array
(
[lat] => 37.4298225
[lng] => -122.0375869
)
[southwest] => Array
(
[lat] => 37.4019577
[lng] => -122.0589996
)
)
[location] => Array
(
[lat] => 37.4199527
[lng] => -122.0584702
)
[location_type] => APPROXIMATE
[viewport] => Array
(
[northeast] => Array
(
[lat] => 37.4298225
[lng] => -122.0375869
)
[southwest] => Array
(
[lat] => 37.4019577
[lng] => -122.0589996
)
)
)
[types] => Array
(
[0] => airport
[1] => transit_station
[2] => establishment
)
)
[2] => Array
(
[address_components] => Array
(
[0] => Array
(
[long_name] => 94043
[short_name] => 94043
[types] => Array
(
[0] => postal_code
)
)
[1] => Array
(
[long_name] => Mountain View
[short_name] => Mountain View
[types] => Array
(
[0] => locality
[1] => political
)
)
[2] => Array
(
[long_name] => California
[short_name] => CA
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[3] => Array
(
[long_name] => United States
[short_name] => US
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Mountain View, CA 94043, USA
[geometry] => Array
(
[bounds] => Array
(
[northeast] => Array
(
[lat] => 37.464087
[lng] => -122.03599
)
[southwest] => Array
(
[lat] => 37.3857439
[lng] => -122.10842
)
)
[location] => Array
(
[lat] => 37.428434
[lng] => -122.0723816
)
[location_type] => APPROXIMATE
[viewport] => Array
(
[northeast] => Array
(
[lat] => 37.464087
[lng] => -122.03599
)
[southwest] => Array
(
[lat] => 37.3857439
[lng] => -122.10842
)
)
)
[types] => Array
(
[0] => postal_code
)
)
[3] => Array
(
[address_components] => Array
(
[0] => Array
(
[long_name] => Santa Clara
[short_name] => Santa Clara
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[1] => Array
(
[long_name] => California
[short_name] => CA
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[2] => Array
(
[long_name] => United States
[short_name] => US
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Santa Clara, CA, USA
[geometry] => Array
(
[bounds] => Array
(
[northeast] => Array
(
[lat] => 37.484637
[lng] => -121.208178
)
[southwest] => Array
(
[lat] => 36.894155
[lng] => -122.202476
)
)
[location] => Array
(
[lat] => 37.2938907
[lng] => -121.7195459
)
[location_type] => APPROXIMATE
[viewport] => Array
(
[northeast] => Array
(
[lat] => 37.484637
[lng] => -121.208178
)
[southwest] => Array
(
[lat] => 36.894155
[lng] => -122.202476
)
)
)
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[4] => Array
(
[address_components] => Array
(
[0] => Array
(
[long_name] => California
[short_name] => CA
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[1] => Array
(
[long_name] => United States
[short_name] => US
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => California, USA
[geometry] => Array
(
[bounds] => Array
(
[northeast] => Array
(
[lat] => 42.0095169
[lng] => -114.131211
)
[southwest] => Array
(
[lat] => 32.5342071
[lng] => -124.4096195
)
)
[location] => Array
(
[lat] => 36.778261
[lng] => -119.4179324
)
[location_type] => APPROXIMATE
[viewport] => Array
(
[northeast] => Array
(
[lat] => 42.0095169
[lng] => -114.131211
)
[southwest] => Array
(
[lat] => 32.5342071
[lng] => -124.4096195
)
)
)
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[5] => Array
(
[address_components] => Array
(
[0] => Array
(
[long_name] => United States
[short_name] => US
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => United States
[geometry] => Array
(
[bounds] => Array
(
[northeast] => Array
(
[lat] => 90
[lng] => 180
)
[southwest] => Array
(
[lat] => -90
[lng] => -180
)
)
[location] => Array
(
[lat] => 37.09024
[lng] => -95.712891
)
[location_type] => APPROXIMATE
[viewport] => Array
(
[northeast] => Array
(
[lat] => 49.38
[lng] => -66.94
)
[southwest] => Array
(
[lat] => 25.82
[lng] => -124.39
)
)
)
[types] => Array
(
[0] => country
[1] => political
)
)
)
[status] => OK
)
*/
?>

Categories