沃梦达 / 编程问答 / php问题 / 正文

PHP Curl 在本地主机上不起作用?

PHP Curl does not work on localhost?(PHP Curl 在本地主机上不起作用?)

本文介绍了PHP Curl 在本地主机上不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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

这篇关于PHP Curl 在本地主机上不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:PHP Curl 在本地主机上不起作用?

基础教程推荐