2012-04-04 89 views
3

我正在尋找將加密變量從HTTP頁面上的網站發送到HTTPS頁面上的另一個網站,我正在使用的代碼是:跨網站使用HTTP和HTTPS接收發送數據

$VARIABLE = 'myvariable'; 

function do_post_request($url, $data, $optional_headers = null) 
{ 
    $params = array('http' => array(
       'method' => 'POST', 
       'content' => $data 
      )); 
    if ($optional_headers !== null) { 
    $params['http']['header'] = $optional_headers; 
    } 
    $ctx = stream_context_create($params); 
    $fp = @fopen($url, 'rb', false, $ctx); 
    if (!$fp) { 
    throw new Exception("Problem with $url, $php_errormsg"); 
    } 
    $response = @stream_get_contents($fp); 
    if ($response === false) { 
    throw new Exception("Problem reading data from $url, $php_errormsg"); 
    } 
    return $response; 
} 

do_post_request('https://localhost/myphpfile.php', $VARIABLE); 

這適用於HTTP而不是HTTPS,但我認爲這只是因爲我在運行WAMP的本地服務器上導致連接被拒絕。

無論如何,我的問題是'myphpfile'中需要什麼來獲取數據傳遞給它?

在此先感謝!

更新:哇,感謝所有的快速回復傢伙!像許多的你的建議,我只是採取了快速看一下捲曲,發現這個躺在附近的谷歌:

$url = 'https://localhost/myphpfile.php'; 

// Initialize session and set URL. 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 

// Set so curl_exec returns the result instead of outputting it. 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 

// Get the response and close the channel. 
$response = curl_exec($ch); 
curl_close($ch); 

我知道VERIFYHOST參數使其不檢查有效的證書,但我想我會等到我離開測試服務器,然後我會得到一個,但是,請你讓我知道如何讓數據被髮送到'myphpfile'?

+0

這是Apache的httpd嗎?如果是這樣,模塊mod_ssl甚至加載? – 2012-04-04 19:35:21

+0

@Khôi:這是WAMP - Windows Apache MySQL PHP。但是我更想知道爲什麼asker使用'stream_get_contents()'而不是做一個簡單的cURL調用。此外,只瀏覽到你的'https:// localhost/myphpfile.php'工作?因爲如果它在你的瀏覽器中不起作用,它不會在你的代碼中工作。 – Crontab 2012-04-04 19:36:40

+0

你不使用捲髮的任何理由? – 2012-04-04 19:36:41

回答

1

建立在你的捲曲的代碼,你真的只需要你curl_exec()調用之前,添加以下行:

// number of POST variables you're passing - your number will likely be different 
curl_setopt($ch,CURLOPT_POST,3); 
// the post variables themselves - make sure the above number matches the number 
// of fields here 
curl_setopt($ch,CURLOPT_POSTFIELDS,"field1=foo&field2=bar&field3=baz"); 
+0

謝謝,我嘗試使用$ HTTP_POST_VARS ['field1']來檢索'foo'作爲測試,但我得到了'未定義變量'錯誤,我應該如何獲取變量? – 2012-04-04 22:07:04

+0

@SamJackson:現在在大多數系統中,使用長變量名已被棄用(我相信)並默認關閉。使用'$ _POST [「field1」]'來檢索POST var'「field1」',在我的例子中,獲得值「'foo」'。 – Crontab 2012-04-05 12:57:51

+1

很好,再次感謝! – 2012-04-05 17:43:27

0

WAMP(Windows?!?)偵聽端口443上的SSL連接嗎?如果是這樣,並且您的Web服務器配置設置爲以類似於HTTP站點的方式正確提供腳本,那麼它應該可以正常工作。

您還需要跳過箍環以確保PHP正在驗證服務器的證書。如果你不這樣做,那麼你基本上可以實現中間人攻擊,從而破壞了使用SSL的全部要點。 cURL可以爲此提供幫助。

0

您需要在appache httpd.conf文件中配置基於SSL的虛擬服務器以偵聽端口443.您還需要爲您的服務器創建一個SSL證書以供使用。

有教程的大量的在線crerating SSL證書和配置基於Apache的SSL虛擬服務器