如何使用 php 发送 HTTPS 帖子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1154172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:15:56  来源:igfitidea点击:

How to send HTTPS posts using php

phphttps

提问by Nico Burns

I'm setting up a custom e-commerce solution, and the payment system I'm using requires me to send HTTPS POSTS.

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

How can I do this using php (and CURL?), is it any different from sending http posts?

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

UPDATE:

更新:

Thanks for your replies, they've been very useful. I assume I will need to purchase an SSL certificate for this to work, and I will obviously do this for the final site, but is there any way for me to test this without buying one?

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

Thanks, Nico

谢谢,尼科

回答by

PHP/Curl will handle the https request just fine. What you may need to do, especially when going against a dev server, is turn CURLOPT_SSL_VERIFYPEER off. This is because a dev server may be self signed and fail the verify test.

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);

回答by VolkerK

You can also use the stream api and http/https context options

您还可以使用流 api 和http/https 上下文选项

$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);

You still need an extension that provides the SSL encryption. That can either be php_openssl or (ifcompiled that way) php_curl.

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

回答by Gumbo

No, there is no much difference. Curl does everything necessary itself.

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

See the examples in the user comments on the curl_setoptreference pagehow it's done.

请参阅参考页面curl_setopt用户评论中的示例如何完成。

回答by Sampson

Similar question: POST to URL with PHP and Handle Response

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

Using the accepted solution (Snoopy PHP Class), you can do something like the following:

使用公认的解决方案(史努比 PHP 类),您可以执行以下操作:

<?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;

?>

回答by Rob Di Marco

If you are using curl, you can pass in the -d switch for your parameters. This results in using an HTTP post. Something like

如果您使用 curl,您可以为您的参数传入 -d 开关。这导致使用 HTTP 帖子。就像是

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

would result in an HTTP post to http://foo.comwith the appropriate parameters

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