2

一般来说,PHP/编程非常新,我一直在尝试运行一个 PHP 文件设置来解析从 Google Search API for Shopping 输出的 Atom 提要。然而,在运行时,代码会输出大量的 enter code heref 错误(警告),所有这些都与 XPath 有关:

Warning: SimpleXMLElement::xpath() [simplexmlelement.xpath]: Undefined namespace prefix in /home/ultradea/public_html/gtest/search.php on line 205

Warning: SimpleXMLElement::xpath() [simplexmlelement.xpath]: xmlXPathEval: evaluation failed in /home/ultradea/public_html/gtest/search.php on line 205

Warning: Invalid argument supplied for foreach() in /home6/legionit/public_html/pricemash/search.php on line 289

我正在使用的完整代码可以在这里找到:search.php

这是我正在使用的输出错误的上传副本 - http://ultra-deals.com/gtest/search.php

这是有问题的具体代码

Lines 204-207
$xml = simplexml_load_string($return);
$result = $xml->xpath('//openSearch:totalResults');
$START = array_shift($xml->xpath('openSearch:startIndex'));
$RESULTS = array_shift($xml->xpath('openSearch:totalResults'));
<...> Line 261
$spelling_suggestion = array_shift($xml->xpath('s:spelling/s:suggestion'));
<...> Lines 273-280
$promos = "";
foreach ($xml->xpath('s:promotions/*') as $item) {
   $promos .= "<td><a href='$item[link]'>$item[description]";
   if ($item[imageLink] != "") {
     $promos .= "<br/><img src='$item[imageLink]'>";
   }
   $promos .= "</td>";
}
<...> Lines 328-335
$NARROW = "";
foreach ($xml->xpath('s:facets/*') as $item) {
 $values = $item->xpath('s:bucket');
 $type = $item['type'];
 $attrib_name = $item['name'];
 if ($attrib_name == "") {
   $attrib_name = $item['property'];  // Default types
 }

每一个都对应于我上传到服务器的 search.php 文件中输出的一条或多条错误消息。我没有使用 XPath 的经验,所以我什至不确定有问题的名称空间前缀是什么。真的很感谢帮助!

4

2 回答 2

0

openSearch 是这里未定义的命名空间。您必须更早地定义它,并且一切都应该开始正常工作。

于 2011-08-25T15:03:58.417 回答
-2

您应该考虑使用 XQuery PHP 扩展来解决您的问题:http ://www.zorba-xquery.com/site2/html/php.html

于 2012-01-09T19:28:50.933 回答