Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Curl does not work on localhost?

I am using MAMP Pro 1.9.4 on Mac OSX
In phpinfo() I see curl is enabled

cURL support    enabled
cURL Information    7.20.0
Age 3
Features
AsynchDNS   No
Debug   No
GSS-Negotiate   No
IDN Yes
IPv6    Yes
Largefile   Yes
NTLM    Yes
SPNEGO  No
SSL Yes
SSPI    No
krb4    No
libz    Yes
CharConv    No
Protocols   dict, file, ftp, ftps, http, https, imap, imaps, ldap, pop3, pop3s, rtsp, smtp, smtps, telnet, tftp
Host    i386-apple-darwin8.11.1
SSL Version OpenSSL/0.9.7l
ZLib Version    1.2.3

My script is to geocode lat long from Google apis. It works online on my hosting providers server but not on localhost.. WHY??

$latlng = "44.3585230889,8.57745766643";
$lang = "it";
$geocodeURL = "http://maps.googleapis.com/maps/api/geocode/json?latlng=$latlng&sensor=false&language=$lang";

$ch = curl_init($geocodeURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($httpCode == 200) {
    $geocode = json_decode($result);

    $location   = $geocode->results[0]->address_components[0]->long_name;
    $city       = $geocode->results[0]->address_components[1]->long_name; 
    $province   = $geocode->results[0]->address_components[2]->long_name; 
    $region     = $geocode->results[0]->address_components[3]->long_name;
    $country    = $geocode->results[0]->address_components[4]->long_name;

    $geo_status = $geocode->status;     
    $geo_str    = "$location, $city, $province, $region, $country";
} else {
    $geo_status = "HTTP_FAIL_$httpCode";
    $geo_str    = "Failed: $geo_status";
}
like image 822
FFish Avatar asked Dec 07 '11 17:12

FFish


2 Answers

It's not about proxy, it's about how to use googleapis.

Add CURLOPT_SSL_ on your code and set them into false

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
like image 68
arbo77 Avatar answered Sep 25 '22 18:09

arbo77


It's probably a firewall issue. Curl by default tries to use port 1080, which is probably not open on your localhost / router / ISP.

like image 42
Dan Crews Avatar answered Sep 22 '22 18:09

Dan Crews