如何使file_get_content()与HTTPS一起工作?

如何使file_get_content()与HTTPS一起工作?

我正在设置信用卡处理,并需要使用一个解决办法的卷曲。当我使用测试服务器(它没有调用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);}


明月笑刀无情
浏览 797回答 3
3回答

温温酱

试试下面的脚本,看看您的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) {   [...]}

浮云间

要允许https包装器,请执行以下操作:这个php_openssl必须存在并启用扩展。allow_url_fopen必须设置为on在php.ini文件中,如果不存在,应该添加以下行:extension=php_openssl.dll allow_url_fopen = On

冉冉说

试试下面的方法。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提供的安全性丢失了。..只使用此代码进行测试/本地开发,从不上网或者其他面向公众的网络。如果此代码有效,则意味着SSL证书不受信任或无法验证,您应该将其作为一个单独的问题加以解决。
打开App,查看更多内容
随时随地看视频慕课网APP