我一直试图从我在服务器上创建的PHP页面访问这个特定的REST服务。我把问题缩小到这两行。我的PHP页面是这样的:

<?php
$response = file_get_contents("https://maps.co.weber.ut.us/arcgis/rest/services/SDE_composite_locator/GeocodeServer/findAddressCandidates?Street=&SingleLine=3042+N+1050+W&outFields=*&outSR=102100&searchExtent=&f=json");

echo $response; ?>

页面在第2行结束,出现以下错误:

Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in ...php on line 2 Warning: file_get_contents(): Failed to enable crypto in ...php on line 2 Warning: file_get_contents(https://maps.co.weber.ut.us/arcgis/rest/services/SDE_composite_locator/GeocodeServer/findAddressCandidates?Street=&SingleLine=3042+N+1050+W&outFields=*&outSR=102100&searchExtent=&f=json): failed to open stream: operation failed in ...php on line 2

我们使用的是Gentoo服务器。我们最近升级到了PHP 5.6版本。这个问题是在升级之后出现的。

当我用https://www.google.com这样的地址替换REST服务时,我发现;我的页面工作得很好。

在前面的尝试中,我设置“verify_peer”=>false,并将其作为参数传递给file_get_contents,如这里所述:file_get_contents忽略verify_peer=>false?但正如作者所指出的;这没什么区别。

我问过我们的一个服务器管理员,php.ini文件中是否存在这些行:

扩展= php_openssl.dll allow_url_fopen = On

他告诉我,因为我们在Gentoo上,所以openssl是在我们构建的时候编译的;在php。ini文件中没有设置。

我还确认allow_url_fopen正在工作。由于这个问题的专门性;我找不到很多有用的信息。你们有人遇到过这样的东西吗?谢谢。


当前回答

修复了macos 12.4 / Mamp 6.6 / Homebrew 3.5.2 / Openssl@3

终端

检查版本

openssl version -a

我的手指指向:

...
OPENSSLDIR: "/opt/homebrew/etc/openssl@3"
...

所以我查看了homebrew的dir /opt/homebrew/etc/openssl@3,找到了cert.pem,并确保我的Mamp当前版本的php的php.ini文件指向了homebrew正确的openssl版本的cert.pem

添加到php.ini

openssl.cafile=/opt/homebrew/etc/openssl@3/cert.pem

其他回答

我通过确保在我的机器上安装了OpenSSL来解决这个问题,然后将这个添加到我的php.ini中:

openssl.cafile=/usr/local/etc/openssl/cert.pem

为我工作,我使用的是PHP 5.6。应该启用Openssl扩展,并且在调用谷歌映射API verify_peer时使false 下面的代码是为我工作。

<?php
$arrContextOptions=array(
    "ssl"=>array(
         "verify_peer"=>false,
         "verify_peer_name"=>false,
    ),
);  
$url = "https://maps.googleapis.com/maps/api/geocode/json?latlng="
      . $latitude
      . ","
      . $longitude
      . "&sensor=false&key="
      . Yii::$app->params['GOOGLE_API_KEY'];

$data = file_get_contents($url, false, stream_context_create($arrContextOptions));

echo $data;
?>

出现此错误的原因是PHP没有受信任的证书颁发机构列表。

PHP 5.6及以上版本尝试自动加载受系统信任的ca。问题是可以解决的。更多信息请参见http://php.net/manual/en/migration56.openssl.php。

PHP 5.5及更早的版本确实很难正确设置,因为您必须在每个请求上下文中手动指定CA包,这是您不希望在代码中出现的事情。 所以我决定在PHP版本< 5.6的代码中禁用SSL验证:

$req = new HTTP_Request2($url);
if (version_compare(PHP_VERSION, '5.6.0', '<')) {
    //correct ssl validation on php 5.5 is a pain, so disable
    $req->setConfig('ssl_verify_host', false);
    $req->setConfig('ssl_verify_peer', false);
}

只是想补充这一点,因为我遇到了同样的问题,我在任何地方都找不到工作(例如下载cacert。Pem文件,在php.ini中设置cafile)

如果你使用NGINX,并且你的SSL证书附带一个“中间证书”,你需要将中间证书文件与你的主“mydomain.com.crt”文件结合起来,它应该可以工作。Apache有一个特定于中间证书的设置,但NGINX没有,所以它必须与常规证书在同一个文件中。

如果您的PHP版本是5,请尝试在终端中输入以下命令来安装cURL:

sudo apt-get install php5-curl