0

I've been trying to perform an XML request. I've faced so many problems that I managed to solve. But this one I couldn't solve. this is the script:

$url ="WebServiceUrl";
$xml="XmlRequest";
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_MUTE, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
            curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($ch);
            curl_close($ch);
 echo $output;

It is giving me this error:

System.InvalidOperationException: Request format is invalid: text/xml. at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

I'm still a noob at this. So go easy on me:) thanks.

Fady Sawan
  • 11
  • 2

2 Answers2

0

Looks like you're sending stuff as text/xml, which is not what it wants. Find the docs for this web service e.g. WSDL stuff if it's there, and find out what data formats it accepts.

Be sure e.g. that it's not really saying it will respond in XML, after receiving a request as standard HTML POST variables.

Matt Gibson
  • 13,675
  • 6
  • 45
  • 75
  • It will respond in XML after receiving the request – Fady Sawan Aug 30 '11 at 12:32
  • well the supporter of the web service told me to try using "application/x-www-form-urlencoded" as content type. but the same error occurs – Fady Sawan Aug 30 '11 at 13:26
  • In that case, it does need standard HTML POST stuff. Check the instructions for CURLOPT_POSTFIELDS [here](http://www.php.net/manual/en/function.curl-setopt.php) and change the $xml variable in your example to match them. – Matt Gibson Aug 30 '11 at 19:40
0

There are two main content types used with the HTTP POST method: application/x-www-form-urlencoded and multipart/form-data.

The content-type determines what the format of the CURLOPT_POSTFIELDS should be. If you are using the default, which is "application/x-www-form-urlencoded" you probably want to use build_http_query() to construct the url encoded query string.

If you are sending non-ASCII data you canpass an associative array with keys that match the field names and values that correspond to the value for the field. Using this technique will cause the request to be issued with a multipart/formdata content-type.

At this point, it sounds like your next step should be figuring out what fields the API is expecting.

application/x-www-form-urlencoded or multipart/form-data?

Community
  • 1
  • 1
gcorne
  • 439
  • 2
  • 5