php - 如何使file_get_contents()与HTTPS一起使用?

标签 php curl https file-get-contents

我正在设置信用卡处理程序,需要对CURL使用替代方法。当我使用测试服务器(未调用SSL URL)时,以下代码可以正常工作,但是现在当我在使用HTTPS的工作服务器上对其进行测试时,它失败,并显示错误消息“无法打开流”。

function send($packet, $url) {
  $ctx = stream_context_create(
    array(
      'http'=>array(
        'header'=>"Content-type: application/x-www-form-urlencoded",
        'method'=>'POST',
        'content'=>$packet
      )
    )
  );
  return file_get_contents($url, 0, $ctx);
}

最佳答案

尝试以下脚本,以查看您的php脚本是否有https包装器。

$w = stream_get_wrappers();
echo 'openssl: ',  extension_loaded  ('openssl') ? 'yes':'no', "\n";
echo 'http wrapper: ', in_array('http', $w) ? 'yes':'no', "\n";
echo 'https wrapper: ', in_array('https', $w) ? 'yes':'no', "\n";
echo 'wrappers: ', var_export($w);


输出应该是这样的

openssl: yes
http wrapper: yes
https wrapper: yes
wrappers: array(11) {
  [...]
}

关于php - 如何使file_get_contents()与HTTPS一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4858261/

相关文章:

PHP 通知 : A non well formed numeric value encountered

php - 直接用 PHP 编写元素的样式是否被认为是不好的做法?

php - 要包含必需的curl header 吗?

javascript - 使用 https 时 Node.js 中的套接字挂起错误

java - 尝试通过 https 和基本身份验证获取 http 响应 - 获取 "Unauthorized"

php - 使用 cron 每小时截屏一次

php - 字符大小写问题

Java cURL 与 HttpURLConnection 的替代方案

PHP file_get_contents() 和查询字符串

c# - 在 APS.NET Core 2 应用程序中使用(并强制执行)HTTPS