我正在尋找將加密變量從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'?
這是Apache的httpd嗎?如果是這樣,模塊mod_ssl甚至加載? – 2012-04-04 19:35:21
@Khôi:這是WAMP - Windows Apache MySQL PHP。但是我更想知道爲什麼asker使用'stream_get_contents()'而不是做一個簡單的cURL調用。此外,只瀏覽到你的'https:// localhost/myphpfile.php'工作?因爲如果它在你的瀏覽器中不起作用,它不會在你的代碼中工作。 – Crontab 2012-04-04 19:36:40
你不使用捲髮的任何理由? – 2012-04-04 19:36:41