-1

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!

3
  • 3
    Rather than try and interpret the var_dump() output of a SimpleXMLElement (which isn't always super clear, especially without indents), why not refer to the API documentation for the response format? Surely that would be a lot clearer. Even something like echo $c->execute($req)->asXML(); might be easier to understand
    – Phil
    Nov 30, 2021 at 0:43
  • Hi! The default value is XML. The value can be set to XML or JSON
    – WSP Web
    Nov 30, 2021 at 12:29
  • Thanks, Phil! Your advice "echo $c->execute($req)->asXML();" help me!
    – WSP Web
    Dec 1, 2021 at 11:44

1 Answer 1

-1

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.

6
  • Hi! If I add this code to my request (My request code + your code) I get "critical error on this website". I had check php-xml module is installed "php-xml-8.0.13-1.el7.remi.x86_64 already installed and latest version"
    – WSP Web
    Nov 30, 2021 at 12:22
  • checks the file error_log in /var/log/httpd, the last records show which part of the code gave error and what error. This can help us solve the problem. If you can post here, I appreciate it! Nov 30, 2021 at 13:14
  • [Tue Nov 30 15:49:40.722498 2021] [php:error] [pid 17643] [client ip-adress:34498] PHP Fatal error: Uncaught Exception: String could not be parsed as XML in /home/admin/web/domain/public_html/wp-content/plugins/insert-php/includes/shortcodes/shortcode-php.php(52) : eval()'d code:22\nStack trace:\n#0 /home/admin/web/domain/public_html/wp-content/plugins/insert-php/includes/shortcodes/shortcode-php.php(52) : eval()'d code(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 ...
    – WSP Web
    Nov 30, 2021 at 13:59
  • The problem is xml structure, it may be that it does not have all the necessary attributes. Do you have a way to publish this XML content? Nov 30, 2021 at 15:13
  • All I get I show in "Server response in SXML". In backend I use "My request code" and response visualization on web page is "Server response in SXML". Maybe you right... maybe response is shorter than must be. All I want is used this response data I have and show only I want with html tags...
    – WSP Web
    Nov 30, 2021 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.