简体   繁体   English

使用 Woocommerce 中的产品数据从发布请求解析 XML 时出错

[英]Error while parsing an XML from a post request with product data in Woocommerce

I would like to ask a question what could be wrong here.我想问一个问题,这里可能有什么问题。

What I am trying to do is to parse an XML from a post request with product data and add them to WooCommerce cart.我要做的是从带有产品数据的发布请求中解析 XML 并将它们添加到 WooCommerce 购物车。

When I am trying to parse the XML from the request in a standalone file on my server it works perfectly and I am able to retrieve, but when I add the same code as a Snippet to WooCommerce page I get the following error.当我尝试从服务器上的独立文件中的请求中解析 XML 时,它运行良好并且我能够检索,但是当我将与片段相同的代码添加到 WooCommerce 页面时,我收到以下错误。

AttValue: " or ' expected attributes construct error Couldn't find end of Start Tag shop line 1 Extra content at the end of the document AttValue: " or ' 预期属性构造错误 找不到开始标记车间线的结尾 1 文档末尾的额外内容

My code snippet here我的代码片段在这里

<?php
libxml_use_internal_errors(true); //enable custom errors
$basketXML=$_REQUEST['basket']; //parsing the post message
$basketXML_decoded = urldecode($basketXML); //decoding decoding post message
$objXmlDocument = simplexml_load_string($basketXML_decoded, 'SimpleXMLElement', LIBXML_NOCDATA); //parsing XML message
if ($objXmlDocument === false) { //check if a valid XML string is loaded.
    echo "\n No orders received \n"; 
    foreach (libxml_get_errors() as $error) {
        echo $error->message;
    }
    exit;
}
$objJsonDocument = json_encode($objXmlDocument); //ecoding to JSON object
$arrOutput = json_decode($objJsonDocument, true); //decoding JSON object into an array.

if (sizeof($arrOutput['item']) == 5) {
   
    $product_id = $arrOutput['item']['productid'];
    $quantity = $arrOutput['item']['amount']; 
   WC()->cart->add_to_cart( $product_id, $quantity );
   
} else {
    foreach ($arrOutput['item'] as $productData) {
    
        $product_id = $productData['productid'];
        $quantity =  $productData['amount'];
      //  $totalAmount += $productData['amount'];
      WC()->cart->add_to_cart( $product_id, $quantity );
    }
}

?>

XML post request sample XML 发布请求样品

<shop request="/test/" testurl="https://test.products.io/" orderId="9GLVYA" orderDate="2022-08-06T11:15:31Z"><item><amount><![CDATA[8]]></amount><productid><![CDATA[35]]></productid><price><![CDATA[100]]></price><name><![CDATA[Speaker]]></name><description><![CDATA[TV]]></description></item><item><amount><![CDATA[2]]></amount><productid><![CDATA[34]]></productid><price><![CDATA[100]]></price><name><![CDATA[TV]]></name><description><![CDATA[TV]]></description></item><item><amount><![CDATA[1]]></amount><productid><![CDATA[36]]></productid><price><![CDATA[100]]></price><name><![CDATA[Laptop]]></name><description><![CDATA[TV]]></description></item></shop>

Any input would be highly appreciated!任何输入将不胜感激!

The issue was solved by removing slash symbols from the decoded XML message.该问题已通过从解码的 XML 消息中删除斜线符号得到解决。

stripslashes($basketXML_decoded)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM