473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

cURL not working while fetching Google API data

2
So, I'm trying to fetch API-data using cURL, but I get the message "fail" from the else-statement in the code below.
The API call is Google geocode for fetching coordinates.

The code:

Expand|Select|Wrap|Line Numbers
  1.  <?php 
  2.     require_once('../db.php');
  3.     $api_key = "somekey";
  4.     $sqlQuery = mysql_query("SELECT `County` FROM `table`"); 
  5.     $ch = curl_init();
  6.  
  7.  
  8.     /* Fetch county */ 
  9.     while($rows = mysql_fetch_array($sqlQuery))  { 
  10.         $countyArr = $rows['County']; 
  11.  
  12.         /* Call google API and save coordinates for each county */ 
  13.         curl_setopt ($ch, CURLOPT_URL, "https://maps.googleapis.com/maps/api/geocode/json?address=".$countyArr.",+CA&key=".$api_key."");
  14.         curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  15.         $json= curl_exec($ch, true);
  16.         $obj = json_decode($json);
  17.  
  18.         if ($obj->status == "OK") {
  19.             $lat = $obj->results->location->lat;
  20.             $lng = $obj->results->location->lng;
  21.             echo $lat;
  22.         } else {
  23.             echo "fail";
  24.         }
  25.     }
  26.     curl_close($ch);
  27. ?> 
  • I intended to use get_file_contents() earlier but it seems like my hosting has deactivated that function.
  • Adding allow_url_fopen = on to php.ini didn't do the trick.
  • It seems like my hosting allows cURL, so that shouldn't be the problem.
  • I've tried to manually go to the API-call and I get a webpage showing the correct JSON-data.
  • The SQL-query seems to be working fine too.
Jun 16 '16 #1
0 1064

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: R4 | last post by:
Hi, I'm working with cUrl and I'm getting strange results. When I run the following script, most of the time I get the error "connect() failed" but sometimes it works and returns the requested...
2
by: yatharth | last post by:
Hi Mike I have Datagrid and i am fetching data around about 1 million into it , i think which is not good deal this makes my system slow.Please tell the way so that i can fetch data in my...
0
by: Nilesh | last post by:
I am developing smart client application which uses web service to retrieve data from HO. The Web Service will get the data from HO database, mark it with status "With PDA" and send the data to the...
0
by: apinti | last post by:
Here is my code, and the problem: $st="...(string to post is formed from different variables read from file)..."; $lngth=strlen($st); $ch = curl_init(); curl_setopt($ch,...
5
by: apinti | last post by:
Here is my code, and the problem: $st="...(string to post is formed from different variables read from file)..."; $lngth=strlen($st); $ch = curl_init(); curl_setopt($ch,...
2
by: anilreddy76 | last post by:
Hi Friends............. I have written one view in oracle once I try to click the "Data" tab in Toad to display the data,it is throwing following error. ORA-01446: cannot select ROWID from, or...
9
by: gstar | last post by:
Hi, Not sure I am in the right place here but thought I would ask an open question. We have an IIS website, mainly ASP files, .NET 1 & 2, this is located in our DMZ. It connects back into the LAN...
1
by: Zak1234 | last post by:
Can any one please help me developing the tool. I have the data in access which I will load every month. My query is I want excel to run as front end and access as back end.. As data grows excel...
11
by: Syed Hadi | last post by:
ow to fetch the data from one server to another server that are connected through lan. ...... Help me out
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.