[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Extracting info from XML on PHP

Posted on 2011-03-20
5
Medium Priority
?
459 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
  • 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month20 days, 12 hours left to enroll

865 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