当国家/地区名称(例如<Country name="US">
)与XML结果中的用户所在国家/地区匹配时,我尝试获取name,PreviewUrl,Description等的值,如下所示:
<Offers>
<Offer Id="656">
<Name>The Hobbit Android</Name>
<Platform>Android Smartphone & Tablet</Platform>
<DailyCap>63</DailyCap>
<PreviewUrl>https://play.google.com/store/apps/details?id=com.kabam.fortress</PreviewUrl>
<TrackingUrl>http://ads.ad4game.com/www/delivery/dck.php?offerid=656&zoneid=7711</TrackingUrl>
<Description>The battle for Middle-earth has just begun! PLAY FOR FREE and join thousands worldwide to drive ..</Description>
<RestrictedTraffic> Social traffic (facebook, etc), Social traffic (facebook, etc) Email MarketingM, Brand Bidding</RestrictedTraffic>
<Countries>
<Country name="US">
<OptIn>SOI</OptIn><Rate>$3.75</Rate>
<EndDate>2013-09-15 16:47:12</EndDate>
</Country>
</Countries>
</Offer>
</Offers>
当我尝试下面的php代码时,我将所有节点连接成一个单独的字符串。当国家/地区名称匹配时,如何将名称,PreviewUrl,Description等作为单个字符串获取?
$xmldoc = new DOMDocument();
$xmldoc->load('http://traffic.ad4game.com/www/admin/offers-api.php');
$xpathvar = new Domxpath($xmldoc);
$queryResult = $xpathvar->query('//Country[@name="US"]/../..');
foreach($queryResult as $result){
echo $result->nodeValue;
}
编辑:至于预期的输出,我希望在获得单个字符串后,我希望它是这样的:
<div id="offer656">
<a href="http://ads.ad4game.com/www/delivery/dck.php?offerid=656&zoneid=7711">
The Hobbit Android</a><br />The battle for Middle-earth has just begun! PLAY FOR FREE and join thousands worldwide to drive ..
</div>
答案 0 :(得分:0)
首先,您需要遍历xpath查询的所有子元素,因此最后添加$sql = "UPDATE {$this->recipientDbTable} SET {$this->recipientDbColumn['result_id']} = '{$hash}' WHERE {$this->recipientDbColumn['id']} = {$this->emailId}";
。
其次,将/*
结果传递到一个数组,您可以在其中下标各个字符串(即nodeValue
,$value[0]
,...)或运行$value[1]
循环数组:
foreach
答案 1 :(得分:0)
使用DOMXpath::evaluate()
而不是DOMXPath::query()
。这两种方法看起来可能相同,但evaluate()
支持返回标量的Xpath表达式。方法的第二个参数是上下文。因此,您可以迭代offer
节点并使用其他Xpath表达式来获取详细信息:
$xmldoc = new DOMDocument();
$xmldoc->load('http://traffic.ad4game.com/www/admin/offers-api.php');
$xpath = new Domxpath($xmldoc);
$offers = $xpath->evaluate('//Offer[Countries/Country[@name="US"]]');
foreach($offers as $offer){
var_dump(
[
'name' => $xpath->evaluate('string(Name)', $offer),
'previewUrl' => $xpath->evaluate('string(PreviewUrl)', $offer)
]
);
}