跨网站使用HTTP和HTTPS接收发送数据

问题描述:

我正在寻找将加密变量从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

建立在你的卷曲的代码,你真的只需要你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

WAMP(Windows?!?)侦听端口443上的SSL连接吗?如果是这样,并且您的Web服务器配置设置为以类似于HTTP站点的方式正确提供脚本,那么它应该可以正常工作。

您还需要跳过箍环以确保PHP正在验证服务器的证书。如果你不这样做,那么你基本上可以实现中间人攻击,从而破坏了使用SSL的全部要点。 cURL可以为此提供帮助。

您需要在appache httpd.conf文件中配置基于SSL的虚拟服务器以侦听端口443.您还需要为您的服务器创建一个SSL证书以供使用。

有教程的大量的在线crerating SSL证书和配置基于Apache的SSL虚拟服务器