开发者

Posing XML string as parameter via cURL

I am trying to post a xml string to a remote perl script via cURL. I want the xml string to be posted as a post parameter 'myxml'. See the code I am using below:

$url = 'http://myurl.com/cgi-bin/admin/xml/xml_append_list_init.pl';
$xml = '<?xml version="1.0" standalone="yes"?>
        <SUB_appendlist>
          <SUB_user>username</SUB_user>
          <SUB_pass>password</SUB_pass>
          <list_id>129</list_id>
          <append>
   开发者_运维技巧         <subscriber>
              <address>test@test.comk</address>
              <first_name>Test</first_name>
              <last_name>Test</last_name>
            </subscriber>
          </append>
        </SUB_appendlist>';

$ch = curl_init(); //initiate the curl session 

curl_setopt($ch, CURLOPT_URL, $url); //set to url to post to
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // tell curl to return data in a variable 
curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml", "Content-length: ".strlen($xml))); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, 'myxml='.urlencode($xml)); // post the xml 
curl_setopt($ch, CURLOPT_TIMEOUT, (int)30); // set timeout in seconds 

$xmlResponse = curl_exec($ch); 
curl_close ($ch); 

However the remote server is not seeing the data in the 'myxml' parameter. And I get the following response back in $xmlResponse

HTTP/1.1 200 OK
Date: Fri, 15 Apr 2011 12:00:44 GMT
Server: Apache/2.2.9 (Debian)
Vary: Accept-Encoding
Content-Length: 0
Content-Type: text/html; charset=ISO-8859-1

I'm not a cURL expert by any measure so I may be doing something in mu cURL request which is obviously wrong. Would appreciate it if anyone can shed any light or spot any problems in this. Hope that is enough information.

Cheers, Adrian.


The body of your message is not text/xml data. It is application/x-www-form-urlencoded data. You have form data containing XML, not plain XML.

Your problem is akin to trying to open MyDoc.zip in MS Word. You have to deal with it as a zip file before dealing with it as Word.

Based on my reading of the PHP manual, you want to remove:

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml", "Content-length: ".strlen($xml))); 

and change the POSTFIELDS line to:

curl_setopt($ch, CURLOPT_POSTFIELDS, array(
    'myxml' => $xml
));


That is not the correct content type for all browsers.

see this article

sometimes the content type for xml is: application/rss+xml

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜