Sending Xml Data Using Http Post with PHP

Send XML with php via post

If you are walking around SOAP services, I strongly recommend you to learn basics once, and then use this great tool again and again. There are many features you can just use, or you will be reinventing the wheel and struggling with generating xml files, parsing xml files, faults etc. Use prepared tools and your life will be easier and your code better (less bugs).

Look at http://www.php.net/manual/en/soapclient.soapcall.php#example-5266 how to consume SOAP webservice. It is not so hard to understand.

Here is some code how you can analyze webserivce. Then map types to classes and just send and receive php objects. You can look for some tool to generate classes automatically (http://www.urdalen.no/wsdl2php/manual.php).

<?php
try
{
$client = new SoapClient('http://b2b.freightquote.com/WebService/QuoteService.asmx?WSDL');

// read function list
$funcstions = $client->__getFunctions();
var_dump($funcstions);

// read some request obejct
$response = $client->__getTypes();
var_dump($response);
}
catch (SoapFault $e)
{
// do some service level error stuff
}
catch (Exception $e)
{
// do some application level error stuff
}

If you will use wsdl2php generating tool, everything is very easy:

<?php

require_once('./QuoteService.php');

try
{
$client = new QuoteService();

// create request
$tracking = new TrackingRequest();
$tracking->BOLNumber = 67635735;

$request = new GetTrackingInformation();
$request->request = $tracking;

// send request
$response = $client->GetTrackingInformation($request);
var_dump($response);
}
catch (SoapFault $e)
{
// do some service level error stuff
echo 'Soap fault ' . $e->getMessage();
}
catch (Exception $e)
{
// do some application level error stuff
echo 'Error ' . $e->getMessage();
}

Generated php code for QuoteService.php you can see here: http://pastie.org/8165331

This is captured communication:

Request

POST /WebService/QuoteService.asmx HTTP/1.1
Host: b2b.freightquote.com
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.4.17
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://tempuri.org/GetTrackingInformation"
Content-Length: 324

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/">
<SOAP-ENV:Body>
<ns1:GetTrackingInformation>
<ns1:request>
<ns1:BOLNumber>67635735</ns1:BOLNumber>
</ns1:request>
</ns1:GetTrackingInformation>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Response

HTTP/1.1 200 OK
Date: Mon, 22 Jul 2013 21:46:06 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Content-Length: 660
Set-Cookie: BIGipServerb2b_freightquote_com=570501130.20480.0000; path=/

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetTrackingInformationResponse xmlns="http://tempuri.org/">
<GetTrackingInformationResult>
<BOLNumber>0</BOLNumber>
<EstimatedDelivery>0001-01-01T00:00:00</EstimatedDelivery>
<TrackingLogs />
<ValidationErrors>
<B2BError>
<ErrorType>Validation</ErrorType>
<ErrorMessage>Unable to find shipment with BOL 67635735.</ErrorMessage>
</B2BError>
</ValidationErrors>
</GetTrackingInformationResult>
</GetTrackingInformationResponse>
</soap:Body>
</soap:Envelope>

Sending XML data using HTTP POST with PHP

you can use cURL library for posting data:
http://www.php.net/curl

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_URL, "http://websiteURL");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "XML=".$xmlcontent."&password=".$password."&etc=etc");
$content=curl_exec($ch);

where postfield contains XML you need to send - you will need to name the postfield the API service (Clickatell I guess) expects

PHP won't receieve POST XML data

Your trying to json_decode XML data. You should use something like SimpleXML.
Instead of...

print_r(json_decode(file_get_contents("php://input"), true));

You should use ...

$xml = new SimpleXMLElement(file_get_contents("php://input"));
echo $xml->asXML();

You should be able to get the information by (for example)...

echo (string)$xml->id;

Retrieve XML from HTTP POST request using php

After much research, I've found the answer to my question. I discovered that after getting the data from the HTTP Post request, I didn't store it as an xml data.

So adding this line of code

 $xml = simplexml_load_string($postData);

after this

 $postData = file_get_contents('php://input');

solved it. I thought I should share it because it might be useful to someone.

PHP - POSTing XML data using cURL-less http web requests to a gateway

I found the solution to the troubles :)

I found out that when the Content-type is NOT application/x-www-form-urlencoded, $_POST in PHP will not have any data to read. Instead, one should access $HTTP_RAW_POST_DATA.

Therefore the code to SEND xml data via POST through file_get_contents is as follows:

<?php

function makeWebRequest()
{
//URL where to send the data via POST
$url = 'http://localhost/connect_to_gateway/receive.php';

//the actual data
$xml = '<?xml version="1.0" encoding="UTF-8"?>' .
'<test>' .
'Hi there!' .
'</test>';

//prepare the HTTP Headers
$content_type = 'text/xml';
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: ' . addslashes($content_type) .'\r\n'
. 'Content-Length: ' . strlen($xml) . '\r\n',
'content' => $xml,
),
);
$context = stream_context_create($options);

/*send the data using a cURL-less method*/
$result = file_get_contents($url, false, $context);
echo 'file_get_contents<br/>';
var_dump($result);
}

//call the function
makeWebRequest();
?>

The method to RECEIVE the data is as follows:

if ($HTTP_RAW_POST_DATA)
{
//create an xml parser and attempt to read it outputting errors if any
$xml_parser=xml_parser_create();
if(!xml_parse_into_struct($xml_parser, $HTTP_RAW_POST_DATA, $vals, $index))
var_dump(array("ERROR"=>sprintf("XML error: %s at line %d",xml_error_string(xml_get_error_code($xml_parser)),xml_get_current_line_number($xml_parser))));
}

Thank you everyone .. phew.. that was painful!

How to HTTP POST a XML File using PHP without cURL?

I think your POST array is wrong

Try:

$xml = file_get_contents('post_xml.xml');
$url = 'http://stg.sa.com/post.asmx/';

$post_data = array(
"xml" => $xml,
);

$stream_options = array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content' => http_build_query($post_data),
),
);

$context = stream_context_create($stream_options);
$response = file_get_contents($url, null, $context);


Related Topics



Leave a reply



Submit