113

我正在设置信用卡处理,需要使用 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);
}
4

13 回答 13

120

允许 https 包装器:

  • php_openssl扩展必须存在并启用
  • allow_url_fopen必须设置为on

在 php.ini 文件中,如果不存在,则应添加以下行:

extension=php_openssl.dll

allow_url_fopen = On
于 2011-09-21T12:11:47.790 回答
94

尝试以下脚本,看看是否有可用于您的 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) {
  [...]
}
于 2009-12-29T17:58:22.867 回答
63
$url= 'https://example.com';

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

$response = file_get_contents($url, false, stream_context_create($arrContextOptions));

这将允许您从 url 获取内容,无论它是 HTTPS

于 2016-08-02T12:25:58.877 回答
58

试试下面的。

function getSslPage($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

注意:这会禁用 SSL 验证,这意味着HTTPS 提供的安全性将丢失。仅将此代码用于测试/本地开发,切勿在 Internet或其他面向公众的网络上使用。如果此代码有效,则意味着 SSL 证书不受信任或无法验证,您应该将其视为单独的问题进行修复。

于 2012-09-16T12:34:57.823 回答
10

这可能是由于您的目标服务器没有有效的 SSL 证书。

于 2009-12-29T16:25:34.817 回答
6

只需在 php.ini 文件中添加两行。

extension=php_openssl.dll

allow_url_include = On

它为我工作。

于 2013-03-09T22:12:16.373 回答
5

如果您已编译为支持 OpenSSL,则从 PHP 4.3.0 开始支持 HTTPS。另外,确保目标服务器具有有效的证书,防火墙允许出站连接并且allow_url_fopen在 php.ini 中设置为 true。

于 2009-12-29T16:26:38.887 回答
5

我有同样的错误。设置allow_url_include = Onphp.ini我修复它。

于 2011-01-28T23:25:11.753 回答
3

就我而言,问题是由于 WAMP 为 CLI 使用了与 Apache 不同的 php.ini,因此您通过 WAMP 菜单进行的设置不适用于 CLI。只需修改 CLI php.ini 即可。

于 2013-01-03T16:22:32.677 回答
2

使用以下代码:

$homepage = file_get_contents("https://www.google.com",false,
                             stream_context_create([
                                'ssl'  => [
                                    'verify_peer'      => false,
                                    'verify_peer_name' => false,
                                ]
                            ])
            );
    
 echo $homepage;
于 2021-01-05T07:26:43.413 回答
1

有时,服务器会根据它在 http 请求标头中看到或未看到的内容(例如适当的用户代理)选择不响应。如果您可以连接浏览器,请获取它发送的标头并在您的流上下文中模仿它们。

于 2009-12-29T18:27:13.243 回答
0

我被 IIS 上的非功能性 https 卡住了。解决了:

file_get_contents('https.. ) 不会加载。

  • 下载https://curl.haxx.se/docs/caextract.html
  • 在 ..phpN.N/extras/ssl 下安装
  • 编辑 php.ini :

    curl.cainfo = "C:\Program Files\PHP\v7.3\extras\ssl\cacert.pem" openssl.cafile="C:\Program Files\PHP\v7.3\extras\ssl\cacert.pem"

最后!

于 2019-11-25T17:25:40.423 回答
-1

检查 URL 是否正常

您问题中的代码可以重写为工作版本:

function send($packet=NULL, $url) {
// Do whatever you wanted to do with $packet
// The below two lines of code will let you know if https url is up or not
$command = 'curl -k '.$url;
return exec($command, $output, $retValue);
}
于 2017-10-13T11:09:56.917 回答