【发布时间】:2012-04-18 13:36:27
【问题描述】:
我希望将加密变量从 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”中获取传递给它的数据吗?
提前致谢!
更新:哇,感谢所有快速回复的家伙!就像你们中的许多人建议的那样,我刚刚快速浏览了一下 cURL 并在 Google 中发现了它:
$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是否已加载? -
@Khôi:这是 WAMP - Windows Apache MySQL PHP。但我想知道更多关于为什么提问者使用
stream_get_contents()而不是进行简单的 cURL 调用。另外,浏览到您的https://localhost/myphpfile.php是否有效?因为如果它在您的浏览器中不起作用,那么它也不会在您的代码中起作用。 -
你不使用 curl 的任何原因?
-
@Khôi 我想是的,在我的 httpd 文件中,'LoadModule ssl_module modules/mod_ssl.so' 行是未散列的。
-
没有理由不使用 cURL,我只是发现在 PHP 中这样做更简单,你能解释一下如何在 cURL 中做到这一点,或者你有什么链接可以给我使用 https 连接吗?