我查看了類似的問題,但沒有解決我遇到的問題。使用php從HTTP POST請求檢索XML
我正在構建Web服務,我想從HTTP POST請求中檢索XML數據,操作數據並返回響應。而寫劇本要考慮下面的信息應該:
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.
我試過,但我的腳本不捕獲從HTTP POST請求的數據。
我的腳本:
$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;
}
這是HTTP POST請求:
<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>
檢索和操縱數據,我的腳本了返回響應後。 這是響應應該如何:
<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>
'$ xml'將是一個字符串。你爲什麼試圖調用它的方法? – Quentin 2014-11-05 09:27:24
@Quentin我不是在$ xml上調用方法;我試圖從xml數據中獲取一些元素 – fidazik 2014-11-05 09:32:23
我不太喜歡PHP-OO,但據我所知'$ xml-> MerchantReference'時不允許使用'$ xml'字符串。 – Quentin 2014-11-05 09:39:28