PHP Curl 在本地主机上不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8419747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Curl does not work on localhost?
提问by FFish
I am using MAMP Pro 1.9.4 on Mac OSX
In phpinfo()
I see curl is enabled
我在 Mac OSX 上使用 MAMP Pro 1.9.4
在phpinfo()
我看到 curl 已启用
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??
我的脚本是从 Google apis 对纬度进行地理编码。它可以在我的托管服务提供商服务器上在线运行,但不能在本地主机上运行。为什么??
$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";
}
采纳答案by Dan Crews
It's probably a firewall issue. Curl by default tries to use port 1080, which is probably not open on your localhost / router / ISP.
应该是防火墙问题。默认情况下,Curl 尝试使用端口 1080,该端口可能未在您的本地主机/路由器/ISP 上打开。
回答by arbo77
It's not about proxy, it's about how to use googleapis.
这不是关于代理,而是关于如何使用 googleapis。
Add CURLOPT_SSL_ on
your code and set them into false
添加CURLOPT_SSL_ on
您的代码并将它们设置为false
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
回答by Brandon Stephens
You may need to set the SSL version and the matching Cipher for $ch
:
您可能需要为以下各项设置 SSL 版本和匹配的密码$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
对于您可以传递的确切参数,CURLOPT_SSLVERSION
请参阅: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:
此外,以下内容还可以显示与 SSL 版本相关的错误,以帮助您找到确切的版本冲突:
$error = curl_error($ch);
echo $error;
More on this command: http://php.net/manual/en/ref.curl.php
有关此命令的更多信息:http: //php.net/manual/en/ref.curl.php
回答by CSM Inc
In MAMP, if you are getting errors when connecting to HTTPS hosts, just set this option:
在 MAMP 中,如果您在连接到 HTTPS 主机时遇到错误,只需设置此选项:
curl_setopt($ch, CURLOPT_SSLVERSION,3);
curl_setopt($ch, CURLOPT_SSLVERSION,3);
The equivalent by command line is:
命令行的等效项是:
curl -k -ssl3 https://www.your-secure-host.com
curl -k -ssl3 https://www.your-secure-host.com
回答by Riyono
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):
如果您在代理后面,则可以尝试添加CURLOPT_PROXY
,例如(假设您的代理位于 192.168.1.2 端口 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.
希望这可以帮助。
回答by Crontab
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)
你真的安装了curl
二进制文件吗?您可以curl
使用 PHP 安装该库,而实际上curl
您的系统上没有二进制文件。转到 shell 提示符并键入curl
. (如果它是 OSX 的标准,那么请原谅我的无知 - 我不是 Mac 人)