【问题标题】:Sending data across websites using from HTTP and receiving in HTTPS使用 HTTP 跨网站发送数据并使用 HTTPS 接收
【发布时间】: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 连接吗?

标签: php http post https


【解决方案1】:

基于您的 cURL 代码,您实际上只需要在您的 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");

【讨论】:

  • 谢谢,我尝试使用 $HTTP_POST_VARS['field1'] 来检索 'foo' 作为测试,但出现“未定义变量”错误,我应该如何获取变量?
  • @SamJackson:不推荐使用长变量名(我相信),并且现在在大多数系统上默认关闭。使用 $_POST["field1"] 检索 POST var "field1",在我的示例中,获取值 "foo"
【解决方案2】:

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

您还需要跳过一些环节来确保 PHP 正在验证服务器的证书。如果你不这样做,那么你基本上是在启用中间人攻击,从而破坏了使用 SSL 的全部意义。 cURL 可以帮助解决这个问题。

【讨论】:

    【解决方案3】:

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

    网上有很多关于创建 SSL 证书和配置基于 Apache SSL 的虚拟服务器的教程

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-13
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多