且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用PHP发送HTTPS帖子

更新时间:2023-02-24 15:37:42

PHP / Curl会正常处理https请求。您可能需要做的事情,特别是在针对开发服务器时,关闭CURLOPT_SSL_VERIFYPEER。这是因为开发服务器可能是自签名的,无法通过验证测试。

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