【问题标题】:How to send HTTPS posts using php如何使用 php 发送 HTTPS 帖子
【发布时间】:2010-11-12 08:31:25
【问题描述】:

我正在设置自定义电子商务解决方案,而我使用的支付系统要求我发送 HTTPS POST。

我如何使用 php(和 CURL?)来做到这一点,它与发送 http 帖子有什么不同吗?

更新:

感谢您的回复,它们非常有用。我假设我需要购买 SSL 证书才能使用它,我显然会为最终站点执行此操作,但是有什么方法可以让我在不购买的情况下进行测试?

谢谢,尼科

【问题讨论】:

  • 如果有人想连接你的服务器,你只需要一个证书,而不是如果你想连接到另一个服务器。
  • 如果您只连接到自己的服务器,则无需购买证书。只要您确定通信渠道的两端,自签名证书与其他证书一样好。此外,如果您通过 https 连接到他们的服务器,他们必须获得证书而不是您。如果公众连接到服务器(通过网络或 API),您只需要购买一个

标签: php https


【解决方案1】:

PHP/Curl 可以很好地处理 https 请求。您可能需要做的是关闭 CURLOPT_SSL_VERIFYPEER,尤其是在针对开发服务器时。这是因为开发服务器可能是自签名的并且无法通过验证测试。

$postfields = array('field1'=>'value1', 'field2'=>'value2');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://foo.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
// Edit: prior variable $postFields should be $postfields;
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // On dev server only!
$result = curl_exec($ch);

【讨论】:

  • @brent baisley 它工作正常,但是当我尝试从某个网站获取时,它会给出“400 Bad Request nginx”在这种情况下该怎么办?
  • @VivekSancheti 可能需要启用 SSL 验证,并创建证书 PEM 文件
  • +1 本地计算机上的选项 CURLOPT_SSL_VERIFYPEER 为我节省了几个小时,因为没有它服务器不想接受我的自签名证书
【解决方案2】:

你也可以使用stream api和http/https context options

$postdata = http_build_query(
  array(
    'FieldX' => '1234',
    'FieldY' => 'yaddayadda'
  )
);

$opts = array(
  'http' => array(
    'method'  => 'POST',
    'header'  => 'Content-type: application/x-www-form-urlencoded',
    'content' => $postdata
  )
);
$context  = stream_context_create($opts);
$result = file_get_contents('https://example.com', false, $context);

您仍然需要提供 SSL 加密的扩展程序。这可以是 php_openssl 或(if 以这种方式编译)php_curl。

【讨论】:

  • 哦,对不起。我应该澄清一下:添加 post 参数对于 http:// 和 https:// 的工作方式相同
  • 您确实暗示了这一点,但我建议您在最后一行编辑https:// 以更直接地回答原始问题。 :) 我想知道这是否是您需要更改的全部,并确认是 PHP 5.3.3。
【解决方案3】:

不,没有太大区别。 Curl 自己做所有必要的事情。

请参阅user comments on the curl_setopt reference page 中的示例如何完成。

【讨论】:

    【解决方案4】:

    如果你使用 curl,你可以为你的参数传入 -d 开关。这导致使用 HTTP 帖子。类似的东西

    curl http://foo.com -d bar=baz -d bat=boo
    

    将导致带有适当参数的 HTTP 发布到 http://foo.com

    【讨论】:

      【解决方案5】:

      类似问题:POST to URL with PHP and Handle Response

      使用公认的解决方案 (Snoopy PHP Class),您可以执行以下操作:

      <?php
      
        $vars = array("fname"=>"Jonathan","lname"=>"Sampson");
        $snoopy = new Snoopy();
      
        $snoopy->curl_path = "/usr/bin/curl";  # Or whatever your path to curl is - 'which curl' in terminal will give it to you.  Needed because snoopy uses standalone curl to deal with https sites, not php_curl builtin.
      
        $snoopy->httpmethod = "POST";
        $snoopy->submit("https://www.somesite.com", $vars);
        print $snoopy->results;
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-06
        • 2011-06-23
        • 1970-01-01
        • 2018-10-05
        • 2022-08-07
        • 2011-11-09
        • 1970-01-01
        相关资源
        最近更新 更多