2009-12-29 80 views
86

我正在设置信用卡处理,并需要使用CURL的解决方法。当我使用测试服务器(不调用SSL URL)时,下面的代码工作正常,但现在当我使用HTTPS在工作服务器上测试它时,它失败并显示错误消息“未能打开流”。如何获取file_get_contents()以使用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); 
} 

回答

80

请尝试以下脚本以查看是否有可用于您的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) { 
    [...] 
} 
+0

这就是说没有https封装器。 – 2009-12-29 18:02:17

+0

@volker这就是为什么我要求allow_url_fopen – Gordon 2009-12-29 18:05:17

+0

那么奇怪的是,它说openssl是关闭的,但根据phpinfo()它编译它,除非我看到它是错误的。 – 2009-12-29 18:09:11

8

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

+0

请求服务器不需要SSL证书。 – ceejayoz 2009-12-29 16:27:18

+12

我没有说请求服务器,我说目标服务器。 – 2009-12-29 16:29:05

+0

这会教我脱脂。嘿!请接受我的道歉和祝福。 – ceejayoz 2009-12-29 16:33:50

5

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

+0

我有支持OpenSSL的PHP​​ 5.2.8。我得到了与此相同的错误,并且目标服务器有一个有效的证书。 – 2009-12-29 16:46:39

+2

是否配置为允许出站https连接的防火墙?通常,运行https的web服务器不会在端口80上运行。除了'打开流失败'之外,还有其他错误吗? – Gordon 2009-12-29 16:51:27

+0

据我所知,这是整个错误: 警告:fopen(https:// ...)[function.fopen]:无法打开流:没有这样的文件或目录在/ home/user /public_html/test.php上线993 – 2009-12-29 17:29:40

1

有时服务器会选择基于它看到或在HTTP请求头(不看不回应,如适当的用户代理)。如果您可以连接浏览器,请抓住它发送的标题并在流上下文中模拟它们。

5

我有同样的错误。设置allow_url_include = Onphp.ini为我修好了。

106

要允许HTTPS包装:

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

在php.ini文件是否应该添加此行不存在:

extension=php_openssl.dll 

allow_url_fopen = On 
+4

allow_url_fopen必须在 – Raffael 2012-08-21 09:29:51

+3

启用openssl扩展工作 - 非常感谢! – Luc 2012-10-21 22:11:48

+2

这应该是正确答案 – OZZIE 2014-02-09 17:35:01

43

请尝试以下操作。

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; 
} 

Note: This disables SSL verification, meaning the security offered by HTTPS is lost. Only use this code for testing/local development, never on the internet or other public-facing networks. If this code works, it means the SSL certificate isn't trusted or can't be verified, which you should look into fixing as a separate issue.

+0

非常有帮助,谢谢! – dotancohen 2013-11-24 08:37:36

+26

为什么要在处理信用卡处理时禁用SSL验证? – Peter 2014-05-12 09:43:26

+0

谢谢,诀窍在这里:curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,FALSE); – 2017-01-06 03:42:10

3

在我的情况下,这个问题使用CLI比Apache一个不同的php.ini是由于WAMP,让你通过WAMP菜单进行设置并不适用于CLI。只需修改CLI php.ini,它就可以工作。

6

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

extension=php_openssl.dll

allow_url_include = On

它为我工作。

+1

你可能的意思是'allow_url_fopen',因为问题涉及到url的开放,不包括。 – 2014-07-03 20:08:41

27
$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

+0

谢谢!这在我的结尾工作。一直在尝试调用FB Open Graph API :) – decodingpanda 2016-09-20 12:50:10

+0

我试图访问我的一个无效证书域,仅用于测试目的,即使这会禁用SSL验证,但这正是我所需要的。 – carla 2017-05-30 17:35:27

-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); 
}