I've been trying to post XML and get response from the server but with no luck.
Here are the conditions on server side:
The following requirements apply to the HTTP request:
Content-Length
header should be present, and the total length of the request should be below 16KB;Content-Type
header should be present, containing the media type value text/xml
;Here is my script:
$url = "http://somedomain.com";
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<Request PartnerID="asasdsadsa" Type="TrackSearch"> <TrackSearch> <Title>love</Title> <Tags> <MainGenre>Blues</MainGenre> </Tags> <Page Number="1" Size="20"/> </TrackSearch> </Request>';
$header = "POST HTTP/1.1 \r\n";
$header .= "Content-type: text/xml \r\n";
$header .= "Content-length: ".strlen($xml)." \r\n";
$header .= "Connection: close \r\n\r\n";
$header .= $xml;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
$data = curl_exec($ch);
echo $data;
if(curl_errno($ch))
print curl_error($ch);
else
curl_close($ch);
This gives me:
HTTP Error 400. The request URL is invalid.
Bad Request - Invalid URL
To post XML using Curl, you need to pass XML data to Curl with the -d command line parameter and specify the data type in the body of the POST request message using the -H Content-Type: application/xml command line parameter.
To send XML to the server using Curl, you need to pass the XML data to Curl with the -d command line option and specify the data type in the body of the POST message using the -H "Content-Type: application/xml" command-line option.
If you want to send XML data to the server, set the Request Header correctly to be read by the sever as XML. xmlhttp. setRequestHeader('Content-Type', 'text/xml'); Use the send() method to send the request, along with any XML data.
The REST API Client Service currently accepts only JSON input when making REST API Create, Read, Update, or Delete requests. It is nevertheless possible to use XML input when making REST API requests.
Does this help?
<?php
$url = "http://stackoverflow.com";
$xml = '<?xml version="1.0" encoding="UTF-8"?><Request PartnerID="asasdsadsa" Type="TrackSearch"> <TrackSearch> <Title>love</Title> <Tags> <MainGenre>Blues</MainGenre> </Tags> <Page Number="1" Size="20"/> </TrackSearch> </Request>';
$headers = array(
"Content-type: text/xml",
"Content-length: " . strlen($xml),
"Connection: close",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
echo $data;
if(curl_errno($ch))
print curl_error($ch);
else
curl_close($ch);
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With