php 如何使用 cURL 发出 HTTPS 请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14448815/
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 07:16:29  来源:igfitidea点击:

How to make an HTTPS request using cURL?

phpcurlhttpsssl-certificate

提问by donparalias

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.

我有 2 个 php 脚本,用于发送一个 xml 文件并捕获它。我正在使用 cURL,一切正常。现在我正在尝试做同样的事情,但使用 HTTP over SSL (HTTPS)。我已经安装了一个带有 XAMPP 的本地服务器,我已经按照这篇文章设置了 SSL:在本地 xampp/apache 服务器上设置 SSL

I am trying to send the xml file like this :

我正在尝试像这样发送 xml 文件:

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

I downloaded the new certificate for cURL from here :

我从这里下载了 cURL 的新证书:

http://curl.haxx.se/ca/cacert.pem

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.

我不确定应该把证书放在哪里,但我把它放在了这个项目的工作区目录中。

The problem now is that the XML file is never sent to the receiver. Any ideas?

现在的问题是 XML 文件永远不会发送到接收器。有任何想法吗?

回答by Ja?ck

The cacert.pemthat you're passing to cURL via the CURLOPT_CAINFOis used to verify certificate authorities, but development servers typically have self signed certificates which are not included in that bundle.

cacert.pem您传递卷曲通过CURLOPT_CAINFO用于验证的证书颁发机构,但开发服务器通常具有不包含在捆绑的自签名的证书。

The first step is to generate your own self signed certificate. This articledescribes 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.

第一步是生成您自己的自签名证书。本文将逐步介绍该过程。确保CSR生成过程中,你正在使用下的预期的服务器名称Common Name (CN),例如ipv4.fiddler

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.

一旦您使用自签名证书(例如server.crt)和密钥(例如server.key)配置了您的 Web 服务器,您需要将前者复制到您的脚本可以访问它的位置。

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";
}

回答by Antonio Piccolo

If I you want to send a sslpost request using Curl, without verify the certificate, you can use the following code:

如果您想使用 发送sslpost 请求Curl,而无需验证证书,您可以使用以下代码:

$data = array(
    'name' => $name,
    'ip' => $ip,
    'text'=> "text"
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,'https://myserver.com/index.php/');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
          http_build_query($data));

// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);