Home > Software engineering >  How interpret Simple XML to HTML with PHP?
How interpret Simple XML to HTML with PHP?

Time:12-02

I get response from the server in Simple XML, but I want get to my page only data I need and show this data in html tags. How I can do it?

My request code:

    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>";

Server response in 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"
    }

I need for example olny strings: Title["product_title"], Price["sale_price"] and TrackID["promotion_link"], how I can get it's? And show on web page only this data:

    <h1>Title</h1>
    <h2>Price</h2>
    <a href>TrackID</a>

Thanks!

CodePudding user response:

try this.

$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>";
}

if not running or error, check if php-xml module as installed.

  • Related