473,549 Members | 3,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cURL not working while fetching Google API data

2 New Member
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_conten ts() 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 1072

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

Similar topics

0
1793
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 page??? <?php $myCurl = curl_init("www.whatever.com"); if(!$myCurl) echo curl_error($myCurl);
2
5184
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 dataset or else so that i dont have to fetch such a large data again and again. My datagrid alows paging.
0
1304
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 PDA via GPRS connectivity. The PDA will insert the data in the SQL CE database. The next Sync from PDA to HO will not bring the records which are...
0
1559
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, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_URL, $url2); $fp = fopen("example_homepage2.txt", "w"); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,20);
5
7404
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, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_URL, $url2); $fp = fopen("example_homepage2.txt", "w"); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,20);
2
4596
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 sample, a view with DISTINCT, GROUP BY, etc. ORA-02063: preceding line from SRTS.PROD but once I run this as query I mean "select * from Viewname"...
9
1722
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 using using port 1433 to our SQL 2000 server. As the Sysadmin I try to ensure our development team only work on the development SQL & IIS boxes...
1
1520
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 size would also grow.. So I want to keep it to access. My issue is I would have so many lines which would be repeating. I want to have checkboxes...
11
1601
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
7446
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7956
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7470
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6041
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1936
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.