TunaFFish
TunaFFish

Reputation: 11302

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";
}

Upvotes: 22

Views: 78407

Answers (8)

Butch
Butch

Reputation: 76

If your OS dont provide cacert.pem file to your local server for SSL certificate validation, you can download it and specify it in php.ini

cacert.pem

php.ini:

[openssl]
; The location of a Certificate Authority (CA) file on the local filesystem
; to use when verifying the identity of SSL/TLS peers. Most users should
; not specify a value for this directive as PHP will attempt to use the
; OS-managed cert stores in its absence. If specified, this value may still
; be overridden on a per-stream basis via the "cafile" SSL stream context
; option.
openssl.cafile=C:\wamp64\cacert.pem

Upvotes: 0

On a local server (eg XAMPP) with a self-signed certificate you must use:

curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);

Note: Despite being unsafe for testing environments it solves the problem.
Remember to activate for production servers !!!

Upvotes: 7

Brandon Stephens
Brandon Stephens

Reputation: 93

You may need to set the SSL version and the matching Cipher for $ch:

curl_setopt($ch, CURLOPT_SSLVERSION, 1);
curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'TLSv1');

For the exact parameters you can pass to CURLOPT_SSLVERSION see: http://php.net/manual/en/function.curl-setopt.php

Also the following can display errors related to SSL versions being used to help find exact version conflict you have:

$error = curl_error($ch);
echo $error;

More on this command: http://php.net/manual/en/ref.curl.php

Upvotes: 4

CSM Inc
CSM Inc

Reputation: 11

In MAMP, if you are getting errors when connecting to HTTPS hosts, just set this option:

curl_setopt($ch, CURLOPT_SSLVERSION,3);

The equivalent by command line is:

curl -k -ssl3 https://www.your-secure-host.com

Upvotes: 1

arbo77
arbo77

Reputation: 599

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

Upvotes: 49

Andronicus
Andronicus

Reputation: 1389

If you are behind a proxy, you could try adding CURLOPT_PROXY, for example (assuming your proxy is on 192.168.1.2 port 3128):

$ch = curl_init($geocodeURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, "192.168.1.2:3128");
$result = curl_exec($ch);

Hope this helps.

Upvotes: 1

Dan Crews
Dan Crews

Reputation: 3597

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

Upvotes: 6

WWW
WWW

Reputation: 9860

Do you actually have the curl binary installed? You can have the curl library installed with PHP without actually having the curl binary on your system. Drop to a shell prompt and type curl. (If it's standard on OSX then please forgive my ignorance - I'm not a Mac guy)

Upvotes: 0

Related Questions