简体   繁体   中英

Using PHP to post an XML buffer to an HTTPS page

I am using php 5.2.9 I have an XML buffer which I need to post to some HTTPS URL.

What is the correct way of doing that? I see various examples on the web, and none of them seem to be working for me: Some define the cURL headers like so:

$headers = array(
        "POST " . $page . " HTTP/1.0",
        "Content-type: text/xml;charset=\"utf-8\"",
        "Accept: text/xml",
        "Cache-Control: no-cache",
        "Pragma: no-cache",
        "SOAPAction: \"run\"",
        "Content-length: ".strlen($buffer),
    );

Where $page holds the request on the server and $buffer contains the XML data.

The actual $buffer is sent as the value as:

curl_setopt($curl, CURLOPT_POSTFIELDS, $buffer);

But I don't see how this can work, as CURLOPT_POSTFIELDS expects its value to be an array and not a buffer.

Then I saw several ways of configuring the SSL aspects of the call:

 curl_setopt($curl, CURLOPT_SSLVERSION,3);

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); Are all of these needed? I saw examples where the following was also set:

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt($curl, CURLOPT_USERPWD, "username:password"); 

Can someone post a complete working example that explains what needs to be done in order to post an XML buffer using cURL to an HTTP URL?

Thanks in advance

A few points:

  • Don't specify the request method with the other headers. Use CURLOPT_CUSTOMREQUEST for that.
  • To send the data you have two options. You can either implement a stream wrapper that reads from $buffer that you then open with fopen and give as a CURLOPT_INFILE option (of course, if the XML is on disk, you can open it directly with fopen ), or, more simply, you define a CURLOPT_READFUNCTION callback.
  • The verify peer part is only necessary if you want to check the validity of the server's certificate (you ought to).
  • Basic authentication is necessary if the server requires basic authentication. Only you can know that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM