我有一个PHP页面,服务在捐赠时发出通知。我的PHP页面应该抓取XML内容并将其解析出来进行处理。
该服务发送以下格式:
http://myserver.com/myphp.com?details= xml data
我有以下代码收听此帖子:
//Load xml from post
$data = file_get_contents('php://input');
$xmlData = simplexml_load_string($data);
//grab mobile number to query mgive for user info
$mnumb= $xmlData->MobileNumber;
$mnumb=ltrim($mnumb,'1');
当服务发出通知时,我收到以下错误。 警告:simplexml_load_string()[function.simplexml-load-string]:实体:第1行:解析器错误:期望开始标记,'<'
中找不到我错过了什么或编码错误?
12-18-2013 855cst 谢谢你...正在取得进展。
我用你的建议#2来获取数据。 当我执行print_r(xmlData)时,屏幕上没有输出。但是,使用chrome developer工具时,我得到以下输出: 数据=%3C%3Fxml +版本%3D%221.0%22 +编码%3D%22utf-8%22%3F%3E%3CGetDonationStatusResult +的xmlns%3Axsi%3D%22http%3A%2F%2Fwww.w3.org%2F2001 %2FXMLSchema实例%22 +的xmlns%3Axsd%3D%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%22%3E%3CResultCode +%3E0%3C%2FResultCode%3E%3CResultText +%2F%3E%3CRecordID + %3E0%3C%2FRecordID%3E%3CMobileNumber +%3E12142911111%3C%2FMobileNumber%3E%3CCarrierID +%3E31002%3C%2FCarrierID%3E%3CCarrierName +%3EAT%26安培%3BT +无线%3C%2FCarrierName%3E%3CDonationStatus +%3EUserAccepted%3C %2FDonationStatus%3E%3CMobileTransactionID +%3E62622731%3C%2FMobileTransactionID%3E%3CDonationMsgGUID +%3E9c17d57f-b54e-488A-8cf5-1c658d1aa618%3C%2FDonationMsgGUID%3E%3CCampaignID +%3E20409%3C%2FCampaignID%3E%3CShortCode +%3E27722%3C%2FShortCode %3E%3CMsgTime +%3E2013-12-17T12%3A53%3A18%3C%2FMsgTime%3E%3CMessageText +%3ELIBERIA + WAP%3C%2FMessageText%3E%3C%2FGetDonationStatusResult%3E&安培; *
您可以看到MobileNumber已发送。但是,当我回应$ mnumb时,我没有输出。我在使用simplexml_load_string($ data)时遗漏了一些东西;抓住MobileNumber?
答案 0 :(得分:0)
目前尚不清楚从哪里获取xml数据。
$xml = $_GET['detail'];
$xml = $_GET['data'];
$xml = $_POST['data'];
$xml = file_get_contents("php://input");
尝试var_dump()数据:
var_dump($_GET, $_POST);
将XML放入变量后,使用DOM + Xpath从中提取值:
$xml = <<<'XML'
<?xml version="1.0" encoding="utf-8"?>
<GetDonationStatusResult xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns:xsd="w3.org/2001/XMLSchema">
<ResultCode >0</ResultCode>
<ResultText />
<RecordID >0</RecordID>
<MobileNumber >19191112222</MobileNumber>
<CarrierID >31002</CarrierID>
<CarrierName >AT&T Wireless</CarrierName>
<DonationStatus >UserAccepted</DonationStatus>
<MsgTime >2013-12-17T20:53:05</MsgTime>
<MessageText >LIBERIA WAP</MessageText>
</GetDonationStatusResult>
XML;
$dom = new DOMDocument();
// try to load the xml
if ($dom->loadXml($xml)) {
$xpath = new DOMXpath($dom);
// read the first MobileNumber element as string
var_dump(
$xpath->evaluate('string(//MobileNumber)')
);
}
输出:
string(11) "19191112222"