This resource is quite uncommon in Techblogs, the main reason why I have decided to startup my blog with this particular post.
Many a time, we are required to integrate a payment gateway, and sometimes we get to provide a request URL to capture data from Gateway Servers. But when the HTTP REQUEST method is POST and content type is TEXT/XML, and the we are unable to process this with the usual $_POST[”] to get our post values and manipulate.
Before moving ahead, I may need to note that $_POST[”] will only return results for FORM-DATA and X-WWW-FORM-URLENCODED but not for APPLICATION/XML or TEXT/XML.
When we have to return results for TEXT/XML or APPLICATION/XML, then we may need to utilise SIMPLEXML to get the NODE values from the result. In doing this, there are two simple lines of code we need to call.
[php]
$postData = file_get_contents(‘php://input’);
$xml=simplexml_load_string($postData);
[/php]
Please take note of the following assumptions for the script to be relevant:
The communication mode is HTTP POST (not SOAP)
The content type is text/xml.
The POST request will contain only XML
The request will be a RAW POST directly to stream and NOT in a parameter.
Here’s my php script to call results and return a response status of “0”.
[php]
MerchantReference)){
$merchRef=$xml->Customers[0]->Customer[0]->MerchantReference;
$custRef=$xml->Customers[0]->Customer[0]->CustReference;
$username=$xml->Customers[0]->Customer[0]->ServiceUsername;
$password=$xml->Customers[0]->Customer[0]->ServicePassword;
$db->setQuery(Check if customer exists in database);
if($db->countResultset() == 0)
{
header(“Content-type: text/xml”);
echo ““;
echo “
echo “
echo “
echo “
echo “
echo “
echo “
echo “
echo “
echo “
echo “
echo “
echo “
echo “
echo “
echo “
exit;
}
?>
[/php]
A sample of the HTTP POST REQUEST
[sourcecode language=”plain”]
[/sourcecode]
The expected response if properly implemented after data manipulation.
[sourcecode language=”plain”]
[/sourcecode]
Now, you may want to submit this data into a database or notify the customer via SMS when this transaction is captured.
This will be discussed in detail on my next post on “file_get_contents”.