如何使用cURL发出HTTPS请求? [英] How to make an HTTPS request using cURL?
问题描述
我有2个php脚本,以发送xml文件并捕获它。我使用cURL和一切都确定。现在我试图做同样,但使用HTTP over SSL(HTTPS)。我已经安装了一个本地服务器与XAMPP和我已设置SSL通过以下这篇文章:在本地xampp / apache服务器上设置SSL。
I have 2 php scripts , to send an xml file and catch it. I am using cURL and everything was ok. Now i am trying to do the same but using HTTP over SSL (HTTPS). I have installed a local server with XAMPP and i have set up SSL by following this post : Setting up SSL on a local xampp/apache server .
我试图像这样发送xml文件:
I am trying to send the xml file like this :
<?php
/*
* XML Sender/Client.
*/
// Get our XML. You can declare it here or even load a file.
$xml = file_get_contents("data.xml");
// We send XML via CURL using POST with a http header of text/xml.
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/cacert.pem");
//i use this line only for debugging through fiddler. Must delete after done with debugging.
curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "https://ipv4.fiddler/iPM/receiver.php");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_REFERER, 'https://ipv4.fiddler/iPM/receiver.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ch_result = curl_exec($ch);
echo "Result = ".$ch_result;
curl_close($ch);
// Print CURL result.
?>
我从这里下载了cURL的新证书:
I downloaded the new certificate for cURL from here :
http://curl.haxx.se/ca/cacert.pem
我不知道应该把证书放在哪里,但我把它放在这个项目的工作区目录中。
I am not sure where i should put the certificate but i put it in my workspace directory of this project.
现在的问题是,XML文件从不会发送到接收器。任何想法?
The problem now is that the XML file is never sent to the receiver. Any ideas?
推荐答案
您通过的 cacert.pem
到cURL通过 CURLOPT_CAINFO
用于验证证书颁发机构,但开发服务器通常有自签名证书,不包括在该捆绑包中。
The cacert.pem
that you're passing to cURL via the CURLOPT_CAINFO
is used to verify certificate authorities, but development servers typically have self signed certificates which are not included in that bundle.
第一步是生成自己的自签名证书。 本文分步介绍了过程。确保在CSR生成期间,您正在使用公用名(CN)
下的预期服务器名称,例如 ipv4.fiddler
。
The first step is to generate your own self signed certificate. This article describes the process step-by-step. Make sure that during the CSR generation you're using the intended server name under the Common Name (CN)
, e.g. ipv4.fiddler
.
使用自签名证书配置您的Web服务器c> server.crt )和键(例如 server.key
),您需要将前者复制到脚本可以访问的位置。
Once you have configured your web server using the self signed certificate (e.g. server.crt
) and key (e.g. server.key
), you need to copy the former to a location that your script can access it.
以下几个基本要素可用于验证整个事物:
The following bare essentials can be used to verify the whole thing:
$ch = curl_init('https://ipv4.fidler');
curl_setopt_array($ch, array(
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_VERBOSE => true,
CURLOPT_CAINFO => '/path/to/server.crt',
));
if (false === curl_exec($ch)) {
echo "Error while loading page: ", curl_error($ch), "\n";
}
这篇关于如何使用cURL发出HTTPS请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!