使用PHP simplexml解析
我试图解析链接中的一些XML,但要更新链接以获取正确的项目信息,则来自正确的使用PHP simplexml解析,php,wordpress,Php,Wordpress,我试图解析链接中的一些XML,但要更新链接以获取正确的项目信息,则来自正确的$product\u id 目标是单击一个按钮(每个按钮都设置了$product\u id编号),然后它将显示产品信息,我将这些信息解析到同一页或另一页的表中(想象一下添加到购物车按钮和购物车显示) 我全局定义了以下内容,但我不确定链接是否正确使用了$product\u id变量: $xml = simplexml_load_file("https://secure.bmtmicro.com/cart?CID=2/WP&
$product\u id
目标是单击一个按钮(每个按钮都设置了$product\u id
编号),然后它将显示产品
信息,我将这些信息解析到同一页或另一页的表中
(想象一下添加到购物车按钮和购物车显示)
我全局定义了以下内容,但我不确定链接是否正确使用了$product\u id
变量:
$xml = simplexml_load_file("https://secure.bmtmicro.com/cart?CID=2/WP&PRODUCTID=' . $product_id . '");
我一直试图在一个应该输出信息的函数中调用$xml
,但它不起作用,所以我只想从头开始故障排除。simplexml\u load\u file()
是否是从URL提取的最佳方式?如果是,我是否正确设置了变量?
(我对PHP相当陌生)。提前谢谢
我用来尝试显示产品信息的功能是:
function print_cart($args = array()) {
#some other code that doesn't involve the $xml
if ($_SESSION['simpleCart'] && is_array($_SESSION['simpleCart'])){
$output .= '
<tr class="cart_item_row">
<th class="cart_item_name_th">' . (__("Product Name")) . '</th><th class"bmt_cart_qty_th">' . (__("quantity")) . '</th><th class="cart_price_th">' . (__("Price")) . '</th><th></th>
</tr>';
foreach ($xml->children() as $product) {
echo $product->productname;
}
$output .= "</table></div>";
$output = apply_filters('after_cart_output', $output);
return $output;
}
函数print_cart($args=array()){
#其他一些不涉及$xml的代码
if($_会话['simpleCart']&&is_数组($_会话['simpleCart'])){
$output.='
“.(“产品名称”)。”(“数量”)。(“价格”)。”
';
foreach($xml->children()作为$product){
echo$product->productname;
}
$output.=“”;
$output=apply_filters('after_cart_output',$output);
返回$output;
}
这是原始XML内容(使用随机产品id及其信息):
22804
XFree86 CD
1.
$15.00
$0.00
$15.00
193
Testproduct-远程密钥生成器
1.
$0.99
$0.50
$0.49
您的foreach
可能拾取了错误的元素。当您使用->children()
时,这是起点的每个子元素。这并不总是您想要的。此代码的作用是循环producttable元素,然后循环其中的行元素(这就是->producttable->row
的来源)
根据您的示例数据,这将
XFree86 CD
Testproduct - Remote Key Generator
首先是基础知识,如果$xml为false,则加载失败。否则,请尝试
echo$xml->asXML();
查看检索到的数据。在$xml上进行var_转储时会发生什么情况?在SimpleXMLElement对象上进行var_转储非常容易出错(通常会遗漏所有类型的数据)。@NigelRen稍后在我的foreach函数中($xml->children()作为$product){echo$product->productname;}
.Productname是我想拉的第一个标签。可能更容易为问题添加更多的代码,更详细地说明您的后续内容以及您到目前为止拥有的内容。谢谢,我想这可能是按钮不显示的原因,但似乎其他地方可能有问题。我已添加了您的代码,并将尝试删除还有什么事on@JeffBerlin我在你的代码中没有看到你试图显示错误的地方button@MaxJones是的,我没有显示这方面的代码,因为我认为这是XML的问题。它是为wordpress设计的,我相信它与某个地方的短代码有关。
foreach ( $xml->producttable->row as $product ) {
echo $product->productname.PHP_EOL;
}
XFree86 CD
Testproduct - Remote Key Generator