php - file_get_contents() : SSL operation failed with code 1, 无法启用加密

标签 php rest ssl file-get-contents

我一直在尝试从我在服务器上创建的 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 ignoring verify_peer=>false?但是就像作者指出的那样;没什么区别。

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

  • extension=php_openssl.dll
  • allow_url_fopen = 开启

他告诉我,因为我们在 Gentoo 上,openssl 在我们构建时编译;并且它没有在 php.ini 文件中设置。

我还确认 allow_url_fopen 正在运行。由于这个问题的特殊性;我没有找到很多信息来寻求帮助。你们中有人遇到过这样的事情吗?谢谢。

最佳答案

这是一个非常有用的链接:

http://php.net/manual/en/migration56.openssl.php

一份官方文档,描述了 PHP 5.6 中打开 ssl 所做的更改 从这里我了解到我应该设置为 false 的另一个参数:“verify_peer_name”=>false

Note: This has very significant security implications. Disabling verification potentially permits a MITM attacker to use an invalid certificate to eavesdrop on the requests. While it may be useful to do this in local development, other approaches should be used in production.

所以我的工作代码是这样的:

<?php
$arrContextOptions=array(
    "ssl"=>array(
        "verify_peer"=>false,
        "verify_peer_name"=>false,
    ),
);  

$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", false, stream_context_create($arrContextOptions));

echo $response; ?>

关于php - file_get_contents() : SSL operation failed with code 1, 无法启用加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56955247/

相关文章:

java - BigInteger 到十六进制冒号表示法

rest - 如何抑制来自 CLI CURL 的请求输出的 header

ssl - 带有 SSL keystore 的 Logstash HTTP 输入

php - 使用 sha256_password(访问被拒绝)时如何使用 PHP 的 mysqli 连接到 MySQL

php - 简单的 CRUD 生成

java - 将REST和Play Framework集成到正在开发的LAMP系统中

rest - 当我的 API 位于我的主网站的子域时,我是否需要启用 CORS?

php - 如何在 mysqli 语句中应用正则表达式

php - 如何在 Doctrine 的 createQuery 的 where 子句中使用字符串?

PHP、JSON 和 JQuery 上传