[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Extracting info from XML on PHP

Posted on 2011-03-20
5
Medium Priority
?
458 Views
Last Modified: 2012-08-14
Hi I am trying to extract info from http://www.mercadolibre.com.mx/jm/searchXml?an_item_id=59860580&an_item_id=58774043 which is an xml. I currently cannot extract anything so I would like to ask for some help.

I am trying to run this code with no response:

<?php

$url = "http://www.mercadolibre.com.mx/jm/searchXml?an_item_id=59860580&an_item_id=58774043";

$xml = simplexml_load_file($url);

echo $xml->response->url;

?>

but actually I would like to print something like

item number 1:
   the id is 58774043
   the title is Usb Wifi Exteriores Gsky 1000mw + Antena 12dbi  + 5mts Cable
   and so on..
can someone help me please.
0
Comment
Question by:degaray
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35176200
Do not use response as the first root it is ommited,
use: echo $xml->url;
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35176213
<?php

$url = "http://www.mercadolibre.com.mx/jm/searchXml?an_item_id=59860580&an_item_id=58774043";

$xml = simplexml_load_file($url);

foreach ($xml->listing->items->item as $val){
    echo"$val->title<br />";
}

?>

Open in new window

0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 35177216
http://www.laprbass.com/RAY_temp_degaray.php

Outputs:
ELEMENT NUMBER 1
ITEM ID = 58774043
ITEM TITLE = Usb Wifi Exteriores Gsky 1000mw + Antena 12dbi  + 5mts Cable
ITEM COSTS $899.00

ELEMENT NUMBER 2
ITEM ID = 59860580
ITEM TITLE = Tarjeta De Red Usb 2 En 1 Wifi Y Bluetooth Con Antena 5dbi
ITEM COSTS $250.00
<?php // RAY_temp_degaray.php
error_reporting(E_ALL);
echo "<pre>";

// READ THE XML STRING INTO AN OBJECT
$url = "http://www.mercadolibre.com.mx/jm/searchXml?an_item_id=59860580&an_item_id=58774043";
$xml = file_get_contents($url);
$obj = SimpleXML_Load_String($xml);
if (!$obj) die("BOGUS XML IN $url");

// ACTIVATE THIS TO SEE THE XML
// var_dump($obj);

$k = 0;
foreach ($obj->listing->items->item as $item)
{
    $k++;
    $i = (string)$item["id"];
    $t = (string)$item->title;
    $c = (string)$item->currency;
    $p = (string)$item->price;
    echo PHP_EOL;
    echo PHP_EOL . "ELEMENT NUMBER $k";
    echo PHP_EOL . "ITEM ID = $i";
    echo PHP_EOL . "ITEM TITLE = $t";
    echo PHP_EOL . "ITEM COSTS $c$p";
}

Open in new window

0
 
LVL 4

Author Closing Comment

by:degaray
ID: 35177698
Thanks Ray_Paseur that was great!!!!!! I appreciate it very much.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35177746
Thanks for the points!  It's a really great question, ~Ray
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question