2012-12-05 62 views
1

我試圖通過HTTPS獲取流的內容,但我必須通過HTTP代理。 我不想使用cURL,而是使用fopen上下文參數。PHP - https流通過http代理

事情是,我不能讓它通過HTTPS工作(雖然HTTP工作正常)。

工作:

$stream = stream_context_create(Array("http" => Array("method" => "GET", 
                 "timeout" => 20, 
                 "proxy" => "tcp://my-proxy:3128", 
                 'request_fulluri' => True 
           ))); 
echo file_get_contents('https://my-stream', false, $context); 

DOES工作(捲曲):

$url = 'https://my-stream'; 
$proxy = 'my-proxy:3128'; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_PROXY, $proxy); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
$curl_scraped_page = curl_exec($ch); 
curl_close($ch); 

echo $curl_scraped_page; 

是否有人知道什麼是錯的第一段代碼?如果它與cURL一起工作,就必須有一種方法來使其與上下文一起工作。 我試圖將上下文選項更改爲一堆不同的值,但沒有運氣。

任何幫助將不勝感激!

謝謝。

+0

從我記得,傳遞給'stream_context_create'陣列中的關鍵是協議將會被使用。嘗試將密鑰從http切換到https。 – sberry

+0

謝謝,但我試過了,沒有運氣。我閱讀了一些代碼片段,在這裏人們發出https請求,而不必將密鑰更改爲「https」。唯一的區別是我使用的是代理服務器:/ – pcdl

+1

將密鑰更改爲「https」不正確! 'https://'調用將使用'http'上下文,然後使用底層'ssl'傳輸上下文。 –

回答

7

您未指定確切的錯誤消息,請嘗試添加ignore_errors => true。但是如果你從Apache獲得400 Bad Request,那麼你可能遇到的問題是服務器名稱指示&主機頭不匹配。還有一個與此相關的一個PHP錯誤:https://bugs.php.net/bug.php?id=63519

試試下面的修復,直到這個bug解決:

$stream = stream_context_create(array(
    'http' => array(
     'timeout' => 20, 
     'proxy' => 'tcp://my-proxy:3128', 
     'request_fulluri' => true 
    ), 
    'ssl' => array(
     'SNI_enabled' => false // Disable SNI for https over http proxies 
    ) 
)); 
echo file_get_contents('https://my-stream', false, $context); 
+0

這對我有用。謝謝! – Dave