繁体   English   中英

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

[英]PHP Curl does not work on localhost?

我在 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

我的脚本是从 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";
}

这不是关于代理,而是关于如何使用 googleapis。

CURLOPT_SSL_ on您的代码CURLOPT_SSL_ on添加CURLOPT_SSL_ on并将它们设置为false

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

应该是防火墙问题。 默认情况下,Curl 尝试使用端口 1080,该端口可能未在您的本地主机/路由器/ISP 上打开。

您可能需要为$ch设置 SSL 版本和匹配的密码:

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

有关您可以传递给CURLOPT_SSLVERSION的确切参数,请参阅: http : //php.net/manual/en/function.curl-setopt.php

此外,以下内容还可以显示与 SSL 版本相关的错误,以帮助您找到确切的版本冲突:

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

有关此命令的更多信息: http : //php.net/manual/en/ref.curl.php

在带有自签名证书的本地服务器(例如 XAMPP)上,您必须使用:

curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false);

注意:尽管对测试环境不安全,但它解决了问题。
记得为生产服务器激活!!!

在 MAMP 中,如果您在连接到 HTTPS 主机时遇到错误,只需设置此选项:

curl_setopt($ch, CURLOPT_SSLVERSION,3);

命令行的等效项是:

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

如果您在代理后面,则可以尝试添加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);

希望这可以帮助。

你真的安装了curl二进制文件吗? 您可以使用 PHP 安装curl库,而实际上您的系统上没有curl二进制文件。 转到 shell 提示符并键入curl (如果它是 OSX 的标准,那么请原谅我的无知 - 我不是 Mac 人)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM