2009-12-29 79 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); 
}