我正在尝试过滤使用cURL获得的xml结果。我现在设置它的方式,以字符串格式保存从curl_exec函数接收的结果,然后将它们放在一个文件中,然后再次从文件中读取它们并尝试用XPATH处理它们。
但是我想在不使用文件保存部分的情况下这样做。
<?php
ini_set('max_execution_time', 0);
$url = "urlhere" ;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$out = curl_exec($ch);
file_put_contents('flickr.xml', $out);
$result = simplexml_load_file('flickr.xml');
$dom = new DOMDocument();
$dom->load('flickr.xml');
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//photo');
echo count($nodes);
?>
文件flickr.xml如下所示:
<rsp stat="ok">
<photos page="1" pages="109" perpage="150" total="16327">
<photo id="15428573530" owner="96092563@N08" secret="534438318e" server="5609" farm="6" title="Saint Sulpice" ispublic="1" isfriend="0" isfamily="0" ownername="Max Sat" dateadded="1414144502" />
<photo id="15415068638" owner="44752279@N02" secret="7ac6e62756" server="5609" farm="6" title="A new life for the city" ispublic="1" isfriend="0" isfamily="0" ownername="Yolanda Miel" dateadded="1414082468" />
<photo id="15608813752" owner="96092563@N08" secret="82fd0ac671" server="3940" farm="4" title="Le lion de la place Saint Sulpice" ispublic="1" isfriend="0" isfamily="0" ownername="Max Sat" dateadded="1414071991" />
<photo id="15406849058" owner="16948241@N08" secret="d1f66da83f" server="3944" farm="4" title="romancing Champs-Élysées" ispublic="1" isfriend="0" isfamily="0" ownername="hannu & hannele" dateadded="1413994013" />
<photo id="15409289537" owner="24406544@N00" secret="d7b7734fb5" server="5601" farm="6" title="Jardin du Luxembourg - Paris (France)" ispublic="1" isfriend="0" isfamily="0" ownername="Meteorry" dateadded="1413985024" />
<photo id="14978720073" owner="96092563@N08" secret="dfbb5c031b" server="3935" farm="4" title="Alien Queen?" ispublic="1" isfriend="0" isfamily="0" ownername="Max Sat" dateadded="1413969753" />
<photo id="15413315170" owner="96092563@N08" secret="4943975ff3" server="3935" farm="4" title="Séparation - Breakup" ispublic="1" isfriend="0" isfamily="0" ownername="Max Sat" dateadded="1413969393" />
<photo id="14200986212" owner="48242216@N08" secret="d0a40248bc" server="2927" farm="3" title="Europe solidaire - Pham Van Nghia" ispublic="1" isfriend="0" isfamily="0" ownername="emmanuelsaussieraffiches" dateadded="1413959936" />
<photo id="15571431276" owner="31944362@N03" secret="c1e2dddce8" server="3949" farm="4" title="Nord !" ispublic="1" isfriend="0" isfamily="0" ownername="Gregouill" dateadded="1413925095" />
<photo id="15243339218" owner="41583834@N03" secret="14618f8a9e" server="3934" farm="4" title="La Promenade plantée, Paris, France" ispublic="1" isfriend="0" isfamily="0" ownername="Grangeburn" dateadded="1413921859" />
<photo id="15590321451" owner="24406544@N00" secret="60a268be25" server="5608" farm="6" title="Jardin du Luxembourg - Paris (France)" ispublic="1" isfriend="0" isfamily="0" ownername="Meteorry" dateadded="1413918288" />
<photo id="15569308866" owner="32959905@N08" secret="97d462b13f" server="3945" farm="4" title="FIAC 2014 : César" ispublic="1" isfriend="0" isfamily="0" ownername="dprezat" dateadded="1413912578" />
</photos>
</rsp>
另外我遇到的另一个问题是$ xpath-&gt;查询(&#39; //照片&#39;)xpath查询没有检索任何内容。我需要检索所有照片的ID。有什么我做错了吗?
修改
修正了XPATH问题,其中包含:
$result = simplexml_load_file("flickr.xml");
$nodes = $result->xpath('//photo');
foreach($nodes as $index=>$node)
{
echo $node['id'];
echo '<br />';
}
答案 0 :(得分:1)
DOMXpath :: query()/ evaluate()返回DOMNodelist,而不是数组。它实现了Traversable,因此您可以将它与foreach一起使用。但它没有实现Countable。您需要使用属性$ length。
$dom = new DOMDocument();
$dom->load('flickr.xml');
$xpath = new DOMXPath($dom);
$nodes = $xpath->evaluate('//photo');
echo $nodes->length;
输出:
12
答案 1 :(得分:1)
如果要计算节点数,可以使用$xp->evaluate
直接使用XPath进行计算:
$result = simplexml_load_file("flickr.xml");
$c = $result->xpath("count(//photo)");
echo "number of photo nodes: $c" . PHP_EOL;
输出:
number of photo nodes: 12
看起来您已经掌握了如何访问属性,但这是添加到底池中的另一种方法:
# gets the attribute "id"
$ids = $xp->evaluate("//photo/@id");
foreach ($ids as $i) {
echo "id: " . $i->nodeValue . PHP_EOL;
}
输出:
id: 15428573530
id: 15415068638
id: 15608813752
id: 15406849058
id: 15409289537
id: 14978720073
id: 15413315170
id: 14200986212
id: 15571431276
id: 15243339218
id: 15590321451
id: 15569308866