php , SimpleXML, while loop
I'm trying to get some information from ebay api and store it in database . I used simple xml to extract the information but I have a small issue as the information is not displayed for some items . if I make a print to the simple_xml I can see very well that the information is provided by ebay api . I have
$items = "220617293997,250645537939,230485306218,110537213815,180519294810";
$number_of_items = count(explode(",", $items));
$xml = $baseClass->getContent("http://open.api.ebay.com/shopping?callname=GetMultipleItems&responseencoding=XML&appid=Morcovar-c74b-47c0-954f-463afb69a4b3&siteid=0&version=525&IncludeSelector=ItemSpecifics&ItemID=$items");
writeDoc($xml, "api.xml");
//echo $xml;
$getvalues = simplexml_load_file('api.xml');
// print_r($getvalue);
$number = "0";
while($number < 6) {
$item_number = $getvalues->Item[$number]->ItemID;
$location = $getvalues->Item[$number]->Location;
$title = $getvalues->Item[$number]->Title;
$price = $getvalues->Item[$number]->ConvertedCurrentPrice;
$manufacturer = $getvalues->Item[$number]->ItemSpecifics->NameValueList[3]->Value;
$model = $getvalues->Item[$number]-&开发者_运维问答gt;ItemSpecifics->NameValueList[4]->Value;
$mileage = $getvalues->Item[$number]->ItemSpecifics->NameValueList[5]->Value;
echo "item number = $item_number <br>localtion = $location<br>".
"title = $title<br>price = $price<br>manufacturer = $manufacturer".
"<br>model = $model<br>mileage = $mileage<br>";
$number++;
}
the above code returns
item number = localtion = title = price = manufacturer = model = mileage = item number = 230485306218 localtion = Coventry, Warwickshire title = 2001 LAND ROVER RANGE ROVER VOGUE AUTO GREEN price = 3635.07 manufacturer = Land Rover model = Range Rover mileage = 76000 item number = 220617293997 localtion = Crawley, West Sussex title = 2004 CITROEN C5 HDI LX RED price = 3115.77 manufacturer = Citroen model = C5 mileage = 76000 item number = 180519294810 localtion = London, London title = 2000 VOLKSWAGEN POLO 1.4 SILVER 16V NEED GEAR BOX price = 905.06 manufacturer = Right-hand drive model = mileage = Standard Car item number = localtion = title = price = manufacturer = model = mileage =
As you can see the information is not retrieved for a few items ... If I replace the $number manually like " $item_number = $getvalues->Item[4]->ItemID;" works well for any number .
I think your mistake is to initialize $number
as a string. Replace $number = "0";
by $number = 0;
to get the first entry working.
As for the last one - maybe there simply are just five entries in the XML result?
Try iterating through the list of items, instead of hard coding a limit into your while loop.
$items = "220617293997,250645537939,230485306218,110537213815,180519294810";
$xml = file_get_contents("http://open.api.ebay.com/shopping?callname=GetMultipleItems&responseencoding=XML&appid=Morcovar-c74b-47c0-954f-463afb69a4b3&siteid=0&version=525&IncludeSelector=ItemSpecifics&ItemID=$items");
$sxml = simplexml_load_string($xml);
foreach ($sxml->Item as $item) {
$out = array();
$out[] = 'Item Number:'. $item->ItemID;
$out[] = 'Location: '. $item->Location;
$out[] = 'Title: '. $item->Title;
$out[] = 'Price: '. $item->ConvertedCurrentPrice;
$out[] = 'Manufacturer: '. $item->ItemSpecifics->NameValueList[3]->Value;
$out[] = 'Model: '. $item->ItemSpecifics->NameValueList[4]->Value;
$out[] = 'Mileage: '. $item->ItemSpecifics->NameValueList[5]->Value;
echo implode('<br />', $out) . '<hr>';
}
精彩评论