我一直在尝试从我在服务器上创建的 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/26148701/