【问题标题】:Allow self-signed PHP on localhost? (Disable SSL?)允许在本地主机上自签名 PHP? (禁用 SSL?)
【发布时间】:2018-05-13 06:20:39
【问题描述】:
<?php
$content = [ 'http' => [ 'method' => 'GET' ], 'ssl' => [ 'verify_peer' => false, 'allow_self_signed'=> true ] ];
$url = 'http://localhost/server-status';
$content = file_get_contents($url);
$first_step = explode( 'Current' , $content );
$second_step = explode("</dl>" , $first_step[1] );

echo $second_step[0];
?>

我正在尝试禁用 SSL,以便我的 PHP 可以从管理面板的服务器状态获取一些统计信息,但 localhost 总是转到 https,这意味着 PHP 无法获取内容,因为 SSL 已启用并且在 localhost 上无效。

在执行 file_get_contents 时如何禁用此功能?

【问题讨论】:

  • 为什么不让您的服务器信任证书? unix.stackexchange.com/questions/90450/…
  • 第一行应该是$context 而不是$content?您也没有将其传递给file_get_contents 调用。或者,您可以使用该上下文调用 stream_context_set_default,这将自动生效。
  • @iainn 我该怎么做,你能发布一个答案来帮助我找出我哪里出错了吗?

标签: php apache .htaccess ssl https


【解决方案1】:

您需要将您创建的流上下文传递给file_get_contents 调用:

$context = stream_context_create(['ssl' => [ 'verify_peer_name' => false, 'verify_peer' => false, 'allow_self_signed'=> true ] ]);

$content = file_get_contents('https://localhost/', false, $context);

我在此处添加了verify_peer_name 选项,但您可能不需要它。

【讨论】:

  • 天才!非常感谢!
猜你喜欢
  • 1970-01-01
  • 2018-10-02
  • 2020-04-30
  • 2013-12-24
  • 1970-01-01
  • 2012-03-05
  • 1970-01-01
  • 2017-12-19
  • 2020-05-17
相关资源
最近更新 更多