我正在记录来自API调用的错误请求/响应。为了将日志存储在数据库中,我想用*代替信用卡号。
请求XML如下所示:
<xml>
<request servicekey="service key" branchcode="branch" language="EN" postalcode="Postal Code" country="CA" email="[email protected]">
<paxes>
<pax id="1" birthdate="19790215" firstname="John" lastname="Smith" />
<pax id="2" birthdate="19800828" firstname="Jane" lastname="Smith" />
</paxes>
<plans>
<plan code="Plan Code" >
<paxes>
<pax id="1" sumins="1200.00" />
<pax id="2" sumins="1480.31" />
</paxes>
</plan>
</plans>
<payments>
<payment amount="246.24" type="VI" ccnum="4111111111111111" ccexp="0711" ccname="John Smith" />
</payments>
</request>
</xml>
从这里,我只需获取付款节点和ccnum属性即可对其进行编辑(xml保存为$ requestXML):
$_req = new DOMDocument();
$_req->loadXML($requestXML);
$_xpathReq = new DOMXPath($_req);
$_reqDom = $_xpathReq->query("/xml/request/payments/payment");
$_reqDom->item(0)->setAttribute('ccnum','*****');
$requestXML = $_req->saveXML();
它按预期方式工作,xml中的抄送号码替换为*
$ responseXML有点不同:
<string xmlns="http://tempuri.org/">
<xml>
<request servicekey="service key" branchcode="branch code" language="EN" postalcode="Postal Code" country="CA" email="[email protected]">
<paxes>
<pax id="1" birthdate="19790215" firstname="John" lastname="Smith" />
<pax id="2" birthdate="19800828" firstname="Jane" lastname="Smith" />
</paxes>
<plans>
<plan code="Plan Code">
<paxes>
<pax id="1" sumins="1200.00" />
<pax id="2" sumins="1480.31" />
</paxes>
</plan>
</plans>
<payments>
<payment amount="246.24" type="VI" ccnum="4111111111111111" ccexp="0711" ccname="John Smith" />
</payments>
</request>
<response>
<errors>
<error>Purchase Card Processing was not approved [-1]:Cancelled: null | CARD: **** | Exp: 1407 | Amount: $246.24</error>
</errors>
</response>
</xml>
</string>
和以前一样,我运行一段非常相似的PHP代码:
$_req = new DOMDocument();
$_req->loadXML($responseXML);
$_xpathReq = new DOMXPath($_req);
$_reqDom = $_xpathReq->query("/string/xml/request/payments/payment");
$_reqDom->item(0)->setAttribute('ccnum','*****');
$responseXML = $_req->saveXML();
但是当我运行它时,我得到Fatal error: Call to a member function setAttribute() on a non-object
指向包含$_reqDom->item(0)->setAttribute('ccnum','*****');
的行
2个xml之间的唯一区别是一个包含在一个字符串节点中,并且有一个额外的节点表示响应。我想我可以解决(只需拉出响应节点并将其附加到请求xml中),但是我现在正是以这种方式工作的时候。
参考方案
问题是由于响应xml中的名称空间。您必须使用XPath选择器注册rootNamespace。以下代码将起作用:
$response = new DOMDocument();
$response->loadXML($responseXml);
$selector = new DOMXPath($response);
// get the root namespace
$rootNamespace = $response->lookupNamespaceUri(
$response->namespaceURI
);
// and register it with $selector. I'm using 'x' as the prefix
// you are free to use a different prefix
$selector->registerNamespace('x', $rootNamespace);
// You won't need to specify the whole path. You could just grab
// all payment node regardless of their location in the xml tree
$result = $selector->query('//x:payment');
foreach($result as $node) {
$node->setAttribute('ccnum', '*****');
}
$responseXml = $response->saveXML();
echo $responseXml;
Div单击与单选按钮相同吗? - php有没有一种方法可以使div上的click事件与表单环境中的单选按钮相同?我只希望下面的div提交值,单选按钮很丑代码输出如下:<input id="radio-2011-06-08" value="2011-06-08" type="radio" name="radio_date…
PHP-复选框组 - php我有一个需要发布的表单复选框组。<input type="checkbox" value="true" checked name="chk0[]"> <input type="checkbox" value="false" name=…
故障排除“警告:session_start():无法发送会话高速缓存限制器-标头已发送” - php我收到警告:session_start()[function.session-start]:无法发送会话缓存限制器-标头已发送(错误输出开始如果我将表单数据提交到其他文件进行处理,则可以正常工作。但是,如果我将表单数据提交到同一页面,则会出现此错误。请建议<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0…
Xpath表达式获取href。不只是锚文本 - php尝试使用xpath表达式来学习它。我找到了一个代码段,并对其进行了一些调整。我想做的是获取页面上的每个链接。$baseurl = "http://www.example.com"; $html = file_get_contents($baseurl); $dom = new DOMDocument(); @$dom->loadHT…
使用PHP包含时的淡入淡出过渡 - php我正在尝试使用jQuery或CSS(或其他方式!)在DIV中包含的php页面上创建淡入淡出或滑动过渡。我四处搜寻,发现了很多淡入淡出过渡的示例,这些实例彼此淡入淡出div或隐藏内容淡入淡出,但是这种情况略有不同。我有一个DIV,其内容由导航栏控制。选中后,每个页面都可以使用PHP成功地包含在div中,但我想使内容淡入和淡出。关于如何在页面更改之间进行漂亮过渡…