Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-12-2013, 10:42 PM   PM User | #1
n_sr6
New to the CF scene

 
Join Date: Jan 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
n_sr6 is an unknown quantity at this point
Question How To Use Curl In Php For Json Results From Google Places Api

I used my API key for querying google places API for obtaining museums around a particular place and obtained the following results in JSON form:
{
"html_attributions" : [],
"results" : [
{
"geometry" : {
"location" : {
"lat" : 15.4954990,
"lng" : 73.82120
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/museum-71.png",
"id" : "1f1425a4048b951552591838918fddcfa92ce0a9",
"name" : "Museum",
"reference" : "CpQBgQAAABj0cBhH-lN_qR75O8cH-CaRqZxZyY98edwHytsuW3hEgA9YX0L9Q5lNSxxJkWKc4hQWZaBie4rAxF6toH1gfcF-EH-8aidpfccqXH7Y6UyN06gNRtaMZtX4KdoQfoDrMvcLPxlpC519Aa6ZFBWII7agaGJ8x3-4Bi7PDIH3nqe6h7ywvQFI12sXh80VE7DhuhIQ-vAeCJylozUNoeZdYEIvLRoUYjxWFbm08-HG-7RIt1JBNcCOsyI",
"types" : [ "museum", "establishment" ],
"vicinity" : "Althino, Panaji (Panjim)"
}
],
"status" : "OK"
}

Then I wrote the following code to obtain only certain data from the JSON result i.e name,type,vicinity
Code:
<?php

$details_url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=15.495602,73.825209&radius=500&types=museum&sensor=false&key=AIzaSyDqEa1fnjGtG4QdAiaekCHKV_tDaN4Nuxo";

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $details_url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   $geoloc = json_decode(curl_exec($ch), true);

  $name=var_dump($geoloc['results'][0]['geometry']['name']);
$types=var_dump($geoloc['results'][0]['geometry']['types']);
$vicinity=var_dump($geoloc['results'][0]['geometry']['types']);


   print $name;
   print $types;
     print $vicinity;

?>
i get the following in my browser when i run the php file:
null
null
null

what do i do next? don't know what the error in the code is. i need to insert the specific data from JSON result that i have got into a database.
n_sr6 is offline   Reply With Quote
Old 01-13-2013, 12:32 AM   PM User | #2
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
You are using var_dump all wrong...Just use plain assignment(or Output buffering which is unnecessary in this case).

PHP Code:
$name$geoloc['results'][0]['geometry']['name'];
$types$geoloc['results'][0]['geometry']['types'];
$vicinity$geoloc['results'][0]['geometry']['types']; 
Okay. Try some debugging.

First, make sure that the cURL function is returning data. Place this code below $geoloc = json_decode(curl_exec($ch), true); Then post the results here.

PHP Code:
echo "<pre>";

print_r($geoloc);

echo 
"</pre>"
And make use of PHP /PHP tags and put your PHP code between them - there's even a button that does that....its mighty hard to read code like plaintext.
__________________
For professional Hosting and Web design.....


NetEssentials.co.uk

Last edited by Redcoder; 01-13-2013 at 12:43 AM..
Redcoder is offline   Reply With Quote
Old 01-13-2013, 09:53 AM   PM User | #3
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
case solved.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Reply

Bookmarks

Tags
curl, google-places api, json, php, wamp

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:51 AM.


Advertisement
Log in to turn off these ads.