how to use curl in php for json results from google places api

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 2949 Views - Last Post: 12 January 2013 - 04:05 PM Rate Topic: -----

#1 nainas9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-December 12

how to use curl in php for json results from google places api

Posted 12 January 2013 - 02:39 PM

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

<?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;

?>  



But it shows error. I have made changes to the following files in WAMP to enable the curl extension(by removing ;)/>

wamp\bin\php\(your php version)\php.ini

wamp\bin\Apache\(apache version)\bin\php.ini

but I am still getting an error when i run the php file with the curl functions. Please help me figure out what i'm doing wrong here.

This post has been edited by Dormilich: 12 January 2013 - 02:40 PM


Is This A Good Question/Topic? 0
  • +

Replies To: how to use curl in php for json results from google places api

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3486
  • View blog
  • Posts: 10,055
  • Joined: 08-June 10

Re: how to use curl in php for json results from google places api

Posted 12 January 2013 - 02:42 PM

View Postnainas9, on 12 January 2013 - 10:39 PM, said:

Please help me figure out what i'm doing wrong here.

It would be a good idea to post the error messages.
Was This Post Helpful? 0
  • +
  • -

#3 nainas9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-December 12

Re: how to use curl in php for json results from google places api

Posted 12 January 2013 - 03:02 PM

View PostDormilich, on 12 January 2013 - 02:42 PM, said:

View Postnainas9, on 12 January 2013 - 10:39 PM, said:

Please help me figure out what i'm doing wrong here.

It would be a good idea to post the error messages.

This is the error message i get in my browser
( ! ) Fatal error: Call to undefined function curl_init() in C:\wamp\www\tut\decode_pan_museum.php on line 7
Call Stack
# Time Memory Function Location
1 0.0485 371680 {main}( ) ..\decode_pan_museum.php:0
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3486
  • View blog
  • Posts: 10,055
  • Joined: 08-June 10

Re: how to use curl in php for json results from google places api

Posted 12 January 2013 - 03:03 PM

seems like you don’t have the cURL extension installed (or at least not restarted the web server).

This post has been edited by Dormilich: 12 January 2013 - 03:03 PM

Was This Post Helpful? 1
  • +
  • -

#5 nainas9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-December 12

Re: how to use curl in php for json results from google places api

Posted 12 January 2013 - 03:07 PM

View PostDormilich, on 12 January 2013 - 03:03 PM, said:

seems like you donít have the cURL extension installed (or at least not restarted the web server).

I am using WAMP server. I have made the changes to the files :
wamp\bin\php\(your php version)\php.ini

wamp\bin\Apache\(apache version)\bin\php.ini
I removed the semicolon before the extension curl.
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3486
  • View blog
  • Posts: 10,055
  • Joined: 08-June 10

Re: how to use curl in php for json results from google places api

Posted 12 January 2013 - 03:09 PM

did you restart the server?
Was This Post Helpful? 1
  • +
  • -

#7 nainas9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-December 12

Re: how to use curl in php for json results from google places api

Posted 12 January 2013 - 03:09 PM

View Postnainas9, on 12 January 2013 - 03:07 PM, said:

View PostDormilich, on 12 January 2013 - 03:03 PM, said:

seems like you donít have the cURL extension installed (or at least not restarted the web server).

I am using WAMP server. I have made the changes to the files :
wamp\bin\php\(your php version)\php.ini

wamp\bin\Apache\(apache version)\bin\php.ini
I removed the semicolon before the extension curl.

And i have also restarted the WAMP server after i made changes to the files and saved the changes in them
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3486
  • View blog
  • Posts: 10,055
  • Joined: 08-June 10

Re: how to use curl in php for json results from google places api

Posted 12 January 2013 - 03:10 PM

do you also have the referenced .dll files in your PHP directory?
Was This Post Helpful? 1
  • +
  • -

#9 nainas9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-December 12

Re: how to use curl in php for json results from google places api

Posted 12 January 2013 - 03:14 PM

View PostDormilich, on 12 January 2013 - 03:10 PM, said:

do you also have the referenced .dll files in your PHP directory?

No i haven't. Which .dll file should I reference in the php directory?
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3486
  • View blog
  • Posts: 10,055
  • Joined: 08-June 10

Re: how to use curl in php for json results from google places api

Posted 12 January 2013 - 03:15 PM

the other way round, if you reference a file (in php.ini) you must have that file at the designated place.
Was This Post Helpful? 1
  • +
  • -

#11 nainas9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-December 12

Re: how to use curl in php for json results from google places api

Posted 12 January 2013 - 03:26 PM

View PostDormilich, on 12 January 2013 - 03:15 PM, said:

the other way round, if you reference a file (in php.ini) you must have that file at the designated place.

I followed this path by accessing the WAMP folder

wamp\bin\php\(your php version)\php.ini

php.ini is the notepad file which opened. In that file i found the following
; Be sure to appropriately set the extension_dir directive.
; 
;extension=php_bz2.dll
;extension=php_curl.dll
;extension=php_dba.dll


I removed the semicolon before ;extension=php_curl.dll to enable loading of curl
What should i do next?


PS: i also saved changes to the notepad file and restarted my webserver

This post has been edited by Dormilich: 12 January 2013 - 03:27 PM

Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3486
  • View blog
  • Posts: 10,055
  • Joined: 08-June 10

Re: how to use curl in php for json results from google places api

Posted 12 January 2013 - 03:28 PM

next you look, whether the file php_curl.dll exists in your computer.


[I am sooo glad that I have the MacPorts repository for that]

This post has been edited by Dormilich: 12 January 2013 - 03:28 PM

Was This Post Helpful? 1
  • +
  • -

#13 nainas9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-December 12

Re: how to use curl in php for json results from google places api

Posted 12 January 2013 - 03:30 PM

Hey i'm really sorry. You were right.:-) I restarted my wamp server again. This time i am getting no errors but i get the following in my browser when i run the php file:
null
null
null

This post has been edited by Dormilich: 12 January 2013 - 03:39 PM
Reason for edit:: removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#14 nainas9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-December 12

Re: how to use curl in php for json results from google places api

Posted 12 January 2013 - 03:37 PM

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.

This post has been edited by Dormilich: 12 January 2013 - 03:38 PM
Reason for edit:: removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#15 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3486
  • View blog
  • Posts: 10,055
  • Joined: 08-June 10

Re: how to use curl in php for json results from google places api

Posted 12 January 2013 - 03:41 PM

it would be more interesting to know, what curl_exec() returned.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2