简体   繁体   中英

PHP HTTP POST Request

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."

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM