1

I need to send an XML string via HTTP POST to another server using the settings below...

POST /xmlreceive.asmx/CaseApplicationZipped HTTP/1.1
Host: www.dummyurl.co.uk
Content-Type: application/x-www-form-urlencoded
Content-Length: length

XMLApplication=XMLstring&byArray=base64string

I'm guessing I need to set this up via cURL or maybe fsockopen.

I've tried the following but not having any luck at getting it to work.

$url = "http://www.dummyurl.co.uk/XMLReceive.asmx/CaseApplicationZipped";

$headers = array(
    "Content-Type: application/x-www-form-urlencoded"//,
);

$post = http_build_query(array('XMLApplication' => $XML, 'byArray' => $base64));

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

curl_close($ch);

echo "response: ".$response;

The remote server gives the following response...

"Object reference not set to an instance of an object."
  • But what does $response print? Maybe you can print the error (with curl_error()) like he said: http://stackoverflow.com/a/1234556/1919749 – ismaestro Mar 27 '15 at 22:27
  • 2
    I don't think you should have the `POST` line in `$headers`. It's not a header, it's the HTTP request, and it's done automatically by cURL based on `CURLOPT_POST`. – Barmar Mar 27 '15 at 22:32
  • I've changed the POST line now, I'm now getting this response... "Cannot convert to System.Byte. Parameter name: type ---> Input string was not in a correct format." –  Mar 27 '15 at 22:39

1 Answers1

0

Not enough rep yet to comment, so I'll post this as an answer.

PHP's cURL automatically send the Host (from $url) and the Content-Length headers, so you don't need to specify those manually.

A handy function exists for building the $post string: http_build_query. It'll handle properly encoding your POST body. It would look something like

$post = http_build_query(array('XMLApplication' => $XML, 'byArray' => $base64));

If you want to log out the headers you received, check the curl_getopt function.

As for the error you received, it seems like you're passing the remote site things it doesn't expect. I can't speak for what you're passing or what the site's expecting, but Input string was not in a correct format seems to imply that your $XML is not formatted correctly, or is being passed as an incorrect parameter.

xathien
  • 812
  • 7
  • 10