如何使用 PHP 将 Simple XML 解释为 HTML?
Posted
技术标签:
【中文标题】如何使用 PHP 将 Simple XML 解释为 HTML?【英文标题】:How interpret Simple XML to HTML with PHP? 【发布时间】:2022-01-06 18:37:24 【问题描述】:我从服务器获得了简单 XML 的响应,但我只想获取我需要的页面数据并在 html 标记中显示这些数据。我该怎么做?
我的请求代码:
include "TopSdk.php";
date_default_timezone_set('Asia/Shanghai');
$c = new TopClient;
$c->appkey = '*******';
$c->secretKey = '***************';
$req = new AliexpressAffiliateProductQueryRequest;
$req->setAppSignature("1111");
$req->setFields("sale_price");
$req->setKeywords("car");
$req->setMaxSalePrice(300);
$req->setMinSalePrice(150);
$req->setPageNo(1);
$req->setPageSize(2);
$req->setSort("SALE_PRICE_ASC");
$req->setTargetCurrency("USD");
$req->setTargetLanguage("en");
$req->setTrackingId("trackID");
echo "<pre>";
var_dump($c->execute($req));
echo "</pre>";
SXML 中的服务器响应:
object(SimpleXMLElement)#2717 (2)
["resp_result"]=>
object(SimpleXMLElement)#2486 (3)
["resp_code"]=>
string(3) "200"
["resp_msg"]=>
string(13) "Call succeeds"
["result"]=>
object(SimpleXMLElement)#2500 (4)
["current_page_no"]=>
string(1) "1"
["current_record_count"]=>
string(1) "2"
["products"]=>
object(SimpleXMLElement)#2493 (1)
["product"]=>
array(2)
[0]=>
object(SimpleXMLElement)#2434 (29)
["app_sale_price"]=>
string(4) "1.50"
["app_sale_price_currency"]=>
string(3) "USD"
["commission_rate"]=>
string(4) "9.0%"
["discount"]=>
string(3) "80%"
["first_level_category_id"]=>
string(2) "34"
["first_level_category_name"]=>
string(32) "Automobiles, Parts & Accessories"
["hot_product_commission_rate"]=>
string(4) "0.0%"
["lastest_volume"]=>
string(1) "0"
["original_price"]=>
string(4) "7.50"
["original_price_currency"]=>
string(3) "USD"
["product_detail_url"]=>
string(53) "https://www.aliexpress.com/item/1005002897604484.html"
["product_id"]=>
string(16) "1005002897604484"
["product_main_image_url"]=>
string(65) "https://ae04.alicdn.com/kf/H251d6cd1fb6b457f86d9418c8fb69bfc4.jpg"
["product_small_image_urls"]=>
object(SimpleXMLElement)#2431 (1)
["string"]=>
array(6)
[0]=>
string(65) "https://ae04.alicdn.com/kf/H251d6cd1fb6b457f86d9418c8fb69bfc4.jpg"
[1]=>
string(65) "https://ae04.alicdn.com/kf/Hef9247dcda18452b86cec742dc6e1512T.jpg"
[2]=>
string(65) "https://ae04.alicdn.com/kf/H0c39946d5ce345feb6f8fe2785157a2aw.jpg"
[3]=>
string(65) "https://ae04.alicdn.com/kf/H46444b25ac824ca48144ce8a41de8df1E.jpg"
[4]=>
string(65) "https://ae04.alicdn.com/kf/Hd5ec838b5f2544b3a21827f2751bcf48y.jpg"
[5]=>
string(65) "https://ae04.alicdn.com/kf/H550518786e1a472ebc1cf0b8cfdc522f2.jpg"
["product_title"]=>
string(163) "Car Parasol Front Windshield Side Window Cover Interior UV-protective Curtain шторки для автомобиля шторка для автомобилей "
["promotion_link"]=>
string(807) "https://s.click.aliexpress.com/s/4BScZUQRCtnCPCxSv0PKwEvDQD4u6TRQDsTl9yY3LPNpjcFJYJYgdvBwDDpTrktMG0Bk7vaKjL6XyPRVpHoWa7nQNlQFkqJPggCvZt8DKU9umyHh6eIX5opg0KuXEYNPi7ieG5LeqJDbqOacEd03kB7TjoPS5lw5oIH5JFWbhzN203U3K2CHxLHglZ3KrbYkGRGOE1cgm0v2oQRL2P5WzsJEV9UY7mMsJJsfwdoEo4YKzpQ4A6hFKY1Q4wYvOojPSfj2bFrEVwvkPSR5Y6FnaSrCZc6K5dZ2jfPZN2c5Uz6ken8DkMcl0at0TpyWMnasTOgxAit4gFga3NDjwESqiUlIktUrZigriucp0TyWz5DmYrGN654flY9yNmmuVez6QNmDok6kjJHM2iFZeBpJBYwSpaEMGvA21M0pjyJqLzq0lO7917WednH9wn4FJlFBHs5yGVX2PaCvU9EH41B3YaDNHDfKWP9aj0XcvBrV85e7h6Zukmu3erQ1h85q1gaClmOGLuF1rrw8w5b4j4NoikPBrUQlBXFQxH8B6bISevyTUl95U1uWA7Gt4uk4ySEEqlsHQLrcVwcF70ySnxuoDQ5dCf6LjaoOs8SOMJRm6I8SippRCihNz7PiLXmfmhyaTO6Bcff1hHQ4xIa4bwasbnh5IL3kF3MobRZBYCinXezrYyhCxlXrqw1GbFYwJ1dc9NGGSwU33s6x8XBpjUzib5y5uE1NVAwTBFCdwOewHMKpebf9e5El5yOT9IQBp2tp2O7zWr"
["relevant_market_commission_rate"]=>
string(4) "3.0%"
["sale_price"]=>
string(4) "1.50"
["sale_price_currency"]=>
string(3) "USD"
["second_level_category_id"]=>
string(9) "200003411"
["second_level_category_name"]=>
string(20) "Interior Accessories"
["shop_id"]=>
string(9) "912064829"
["shop_url"]=>
string(42) "https://www.aliexpress.com/store/912064829"
["target_app_sale_price"]=>
string(4) "1.50"
["target_app_sale_price_currency"]=>
string(3) "USD"
["target_original_price"]=>
string(4) "7.50"
["target_original_price_currency"]=>
string(3) "USD"
["target_sale_price"]=>
string(4) "1.50"
["target_sale_price_currency"]=>
string(3) "USD"
[1]=>
object(SimpleXMLElement)#2432 (29)
["app_sale_price"]=>
string(4) "1.35"
["app_sale_price_currency"]=>
string(3) "USD"
["commission_rate"]=>
string(4) "9.0%"
["discount"]=>
string(3) "73%"
["first_level_category_id"]=>
string(2) "34"
["first_level_category_name"]=>
string(32) "Automobiles, Parts & Accessories"
["hot_product_commission_rate"]=>
string(4) "0.0%"
["lastest_volume"]=>
string(1) "0"
["original_price"]=>
string(4) "5.00"
["original_price_currency"]=>
string(3) "USD"
["product_detail_url"]=>
string(53) "https://www.aliexpress.com/item/1005003402966179.html"
["product_id"]=>
string(16) "1005003402966179"
["product_main_image_url"]=>
string(65) "https://ae04.alicdn.com/kf/Hd263fbbd1dad4d49a3a3277261c6c408L.jpg"
["product_small_image_urls"]=>
object(SimpleXMLElement)#2431 (1)
["string"]=>
array(5)
[0]=>
string(65) "https://ae04.alicdn.com/kf/Hd263fbbd1dad4d49a3a3277261c6c408L.jpg"
[1]=>
string(65) "https://ae04.alicdn.com/kf/H3595b1b670c04d8b95307ad54f30ee5ac.jpg"
[2]=>
string(65) "https://ae04.alicdn.com/kf/Hbc1076a0bbca4f91a60d8eb27f9392547.jpg"
[3]=>
string(65) "https://ae04.alicdn.com/kf/H999a513e61be49d2ad374fa87e4f34897.jpg"
[4]=>
string(65) "https://ae04.alicdn.com/kf/H7e29d629b6d04ec8870f166a71cf87212.jpg"
["product_title"]=>
string(124) "2022 New Car Interior Accessories Car Perfume Aromatherapy Cute Crown Teddy Bear Air Outlet Plaster Bear Ornament Decoration"
["promotion_link"]=>
string(807) "https://s.click.aliexpress.com/s/4BScZUQRCtnCPCxSv0PKwEvDQD4u6TRQDsTl9yY3LPNpjcFJYJYgdvBwDDpTrktMG0Bk7vaKjL6XyPRVpHoWa7nQNlQFkqJPggCvZt8DKU9umyHh6eIX5opg0KuXEYNPi7ieG5LeqJDbqOacEd03kB7TjoPS5lw5oIH5JFWbhzN203U3K2CHxLHglZ3KrbYkGRGOE1cgm0v2oQRL2P5WzsJEV9UY7mMsJJsfwdoEo4YKzpQ4A6hFKY1Q4wYvOojPSfj2bFrEVwvkPSR5Y6FnaSrCZc6K5dZ2jfPZN2c5Uz6ken8DkMcl0at0TpyYdHF0HQtA8jRRgCMGPA1XaVUh1IydbFgJOCvwetULs4d0J8ib9Xni5t3QrUNsxQ3lSARntkxnshpruYkDmzb3HJ3sf9GyH5rTL3OTC0dpG3XfbyCmQI7sE1rDkdWSBbIvIHJXWaawBxtuhmqr6WfjZDNeYY0JfdgUsh9F8OqLed1uePoqJHK8FdFG6GAnXwZkCbS84viXRSeHP4EjjjC4Z7gtBzNveIjurnHopaqkY4FNXnJzdGJwnudZyXpX7pV7jkaygdr7GnlfsroGM4P1e8GnFgcimWdqfV0vg71vTxlBJmVDSrtmndZVpTXu3dfIZbjuIyWZWwlCrKCxWbqsppQYItz8662L6l9uKNJLIoKipN0ZTyWkGA2XYBg9al3sPUsO39aTK7ZnxOr3SWOk8Zo6Ua3q3v6UemjvLqqruSU46TCFydPTWxg9wegSuYctUTY0MgdLen"
["relevant_market_commission_rate"]=>
string(4) "3.0%"
["sale_price"]=>
string(4) "1.35"
["sale_price_currency"]=>
string(3) "USD"
["second_level_category_id"]=>
string(9) "200003411"
["second_level_category_name"]=>
string(20) "Interior Accessories"
["shop_id"]=>
string(9) "912521256"
["shop_url"]=>
string(42) "https://www.aliexpress.com/store/912521256"
["target_app_sale_price"]=>
string(4) "1.35"
["target_app_sale_price_currency"]=>
string(3) "USD"
["target_original_price"]=>
string(4) "5.00"
["target_original_price_currency"]=>
string(3) "USD"
["target_sale_price"]=>
string(4) "1.35"
["target_sale_price_currency"]=>
string(3) "USD"
["total_record_count"]=>
string(6) "472235"
["request_id"]=>
string(12) "iirdj5wwp91x"
我需要例如 olny 字符串:Title["product_title"]、Price["sale_price"] 和 TrackID["promotion_link"],我怎样才能得到它?并仅在网页上显示此数据:
<h1>Title</h1>
<h2>Price</h2>
<a href>TrackID</a>
谢谢!
【问题讨论】:
与其尝试解释SimpleXMLElement
的var_dump()
输出(这并不总是非常清楚,尤其是没有缩进),为什么不参考API 文档了解响应格式?那肯定会清楚很多。甚至像echo $c->execute($req)->asXML();
这样的东西可能更容易理解
嗨!默认值为 XML。该值可以设置为 XML 或 JSON
谢谢,菲尔!您的建议“echo $c->execute($req)->asXML();”帮帮我!
【参考方案1】:
试试这个。
$content = "xml content";
$xml = new \SimpleXMLElement($content);
foreach($xml->resp_result->result->products->product as $product)
echo "<h1>" . $product->product_title . "</h1>";
echo "<h2>" . $product->sale_price . "</h2>";
echo "<a href>" . $product->promotion_link . "</a>";
如果没有运行或出错,请检查 php-xml 模块是否已安装。
【讨论】:
嗨!如果我将此代码添加到我的请求中(我的请求代码 + 您的代码),我会收到“此网站上的严重错误”。我检查了 php-xml 模块是否已安装“php-xml-8.0.13-1.el7.remi.x86_64 已安装且最新版本” 检查/var/log/httpd 中的error_log 文件,最后的记录显示代码的哪一部分出错了,什么错误。这可以帮助我们解决问题。如果你能在这里发帖,我很感激! [2021 年 11 月 30 日星期二 15:49:40.722498] [php:error] [pid 17643] [client ip-adress:34498] PHP 致命错误:未捕获的异常:字符串无法解析为 XML在 /home/admin/web/domain/public_html/wp-content/plugins/insert-php/includes/shortcodes/shortcode-php.php(52) : eval()'d 代码:22\n堆栈跟踪:\n# 0 /home/admin/web/domain/public_html/wp-content/plugins/insert-php/includes/shortcodes/shortcode-php.php(52) : eval()'d 代码(22): SimpleXMLElement->__construct( )\n#1 /home/admin/web/domain/public_html/wp-content/plugins/insert-php/includes/shortcodes/shortcode-php.php(52): eval()\n#2 ... 问题是xml结构,可能是它没有所有必要的属性。你有办法发布这个 XML 内容吗? 所有我得到的都显示在“SXML 中的服务器响应”中。在后端,我使用“我的请求代码”,网页上的响应可视化是“SXML 中的服务器响应”。也许你是对的……也许反应比必须的要短。我想要的只是使用我拥有的这个响应数据,并且只显示我想要的 html 标签......以上是关于如何使用 PHP 将 Simple XML 解释为 HTML?的主要内容,如果未能解决你的问题,请参考以下文章
在 Perl 中使用 XML::Simple 将哈希转换为 XML 后内容丢失
XML :: Simple为大型XML返回“Out of memory”错误