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/26148701/

相关文章:

api - REST 等基于 Web 的 API 与 TLS/SSL 之间有何关系?

php - Symfony2 Form + AngularJS for REST API = 表单验证额外字段错误

php - 使用 Silex 框架加密数据

ssl - 数字签名认证法律问题

symfony - Goutte Scrape 登录到 https 安全网站

php - mysql - 已超出 'max_user_connections' 资源

php - 显示不同 mysql 表中的最新条目

php - 如何在资源/访问受限的共享主机上使用 ffmpeg 和 laravel 5.7

javascript - 如何从 MYSQL 反序列化查询结果?

c# - 获取 0 的 HttpStatusCode