Using PHP to post an XML buffer to an HTTPS page
I am using php 5.2.9 I have an XML 开发者_JAVA技巧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 withfopen
and give as aCURLOPT_INFILE
option (of course, if the XML is on disk, you can open it directly withfopen
), or, more simply, you define aCURLOPT_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.
精彩评论