如何使用 php 发送 HTTPS 帖子

发布于 2024-07-27 00:38:01 字数 250 浏览 0 评论 0原文

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

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

更新:

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

谢谢,尼科

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

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

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?

Thanks, Nico

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

云朵有点甜 2024-08-03 00:38:01

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

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.

$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);
故事还在继续 2024-08-03 00:38:01

您还可以使用流 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);

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

You can also use the stream api and 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);

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

原谅我要高飞 2024-08-03 00:38:01

不,没有太大区别。 Curl 会自行完成所有必要的工作。

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

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

See the examples in the user comments on the curl_setopt reference page how it’s done.

孤蝉 2024-08-03 00:38:01

如果您使用的是curl,则可以传入-d 开关作为参数。 这会导致使用 HTTP post。 类似的事情

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

会导致使用适当的参数将 HTTP post 发送到 http://foo.com

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 http://foo.com -d bar=baz -d bat=boo

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

榆西 2024-08-03 00:38:01

类似的问题:使用 PHP 发布到 URL 并处理响应

使用公认的解决方案(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;

?>

Similar question: POST to URL with PHP and Handle Response

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

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

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文