views:

26

answers:

1

Hi, 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: 

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.
Artefacto
I already the XML buffer in the $buffer variables. I don't want to have the code read it from the the disk. According to the callback solution, this is the what the function should look like:string read_callback (resource ch, resource fd, long length)where fd is the file descriptor passed to CURL by CURLOPT_INFILE option. Once again, I don't want to work with files. Isn't there a more simple solution?
@jason What the docs say is "The name of a callback function where the callback function takes two parameters. The first is the cURL resource, and the second is a string with the data to be read. The data must be read by using this callback function. Return the number of bytes read. Return 0 to signal EOF." So no fd.
Artefacto