Reputation: 433
I have checked similar questions but none solves the problem I'm facing.
I'm building a web service and I want to retrieve the XML data from a HTTP POST request, manipulate the data and return a response. The information below ought to be considered while writing the script:
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.
I've tried but my script isn't capturing the data from the HTTP POST request.
my script:
$postData = file_get_contents('php://input');
if(!empty($xml->MerchantReference)){
$merchRef = (string)$xml->MerchantReference;
$custRef = (int)$xml->CustReference;
$username = (string)$xml->ServiceUsername;
$password = (string)$xml->ServicePassword;
$db->setQuery(Check if customer exists in database);
if($db->countResultset() == 0)
{
header("Content-type: text/xml");
echo "<?xml version='1.0' encoding='UTF-8'?>";
echo "<CustomerInformationResponse>";
echo "<MerchantReference>".$merchRef."</MerchantReference>";
echo "<Customers>";
echo "<Customer>";
echo "<Status>0</Status>";
echo "<CustReference>".$custRef."</CustReference>";
echo "<FirstName/>";
echo "<LastName/>";
echo "<OtherName/>";
echo "<Email/>";
echo "<Phone/>";
echo "<ThirdPartyCode/>";
echo "<StatusMessage>Customer is valid</StatusMessage>";
echo "</Customer>";
echo "</Customers>";
echo "</CustomerInformationResponse>";
exit;
}
This is the HTTP POST request:
<CustomerInformationRequest xmlns:ns2="http://test.url.com/" xmlns:ns3="http://www.w3.org/2003/05/soap-envelope">
<ServiceUrl>MY URL</ServiceUrl>
<ServiceUsername>12345</ServiceUsername>
<ServicePassword>abcdef</ServicePassword>
<RouteId>HTTPGENERICv31</RouteId>
<Service>bill</Service>
<MerchantReference>123456</MerchantReference>
<CustReference>abcdef</CustReference>
<PaymentItemCategoryCode/>
<RequestReference/>
<TerminalId/>
<Amount>0</Amount>
<FtpUsername/>
<FtpPassword/>
</CustomerInformationRequest>
After retrieving and manipulating the data, my script out to return a response. This is how the response should be:
<CustomerInformationResponse>
<MerchantReference>3527</MerchantReference >
<Customers>
<Customer>
<Status>0</Status>
<CustReference>4565</CustReference>
<FirstName></FirstName>
<LastName></LastName>
<OtherName></OtherName>
<Email></Email>
<Phone></Phone>
<ThirdPartyCode/>
<StatusMessage>Customer is Valid</StatusMessage>
</Customer>
</Customers>
</CustomerInformationResponse>
Upvotes: 3
Views: 17224
Reputation: 41
$merchRef = (string)$xml->MerchantReference; //This did not work for me. However check how I formatted the code to get the xml nodes correctly
<?php
$postData = file_get_contents('php://input');
$xml=simplexml_load_string($postData);
////////////////
$merchRef=$xml->Customers[0]->Customer[0]->MerchantReference;
$custRef=$xml->Customers[0]->Customer[0]->CustReference;
?>
Upvotes: 4
Reputation: 433
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.
Upvotes: 10