2010-06-17 7 views
0

나는 eBay API에서 정보를 얻고 데이터베이스에 저장하려고합니다. 정보를 추출하기 위해 간단한 xml을 사용했지만 일부 항목에는 정보가 표시되지 않기 때문에 작은 문제가 있습니다. simple_xml에 인쇄하면 이베이 api에서 제공하는 정보를 볼 수 있습니다. 내가 수동으로 $ ITEM_NUMBER = $ "처럼 $ 번호를 교체 할 경우 나는 ... 당신이 정보는 몇 가지 항목에 대한 검색되지 않습니다 볼 수 있듯이PHP, SimpleXML, while 루프

$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]->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++; 

} 

위의 코드

 
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 = 

를 반환해야 getvalues-> Item [4] -> ItemID; " 모든 번호에서 잘 작동합니다.

답변

1

실수로 $number을 문자열로 초기화하는 것이 좋습니다. $number = "0";$number = 0;으로 바꾸면 첫 번째 입력이 작동합니다.

마지막으로는 XML 결과에 단지 5 개의 항목 만있을 수 있습니다.

+0

대단히 감사합니다. 귀하의 답변을 "수락 됨"으로 표시했습니다. 괜찮 으면 제조사, 모델 및 마일리지를 얻기위한 "안정적인"솔루션을 알려주십시오. eBay API는 이름 및 값으로 트리에이 정보를 표시하지만 일부 항목에는 "특성"이 더 많으므로 배열 번호가 다른 경우가 있습니다. http://pastebin.com/NrvUdg8F xml 문서 예제를 보시고 간단한 xml의 프린트는 http://pastebin.com/hY40YrWV – Michael

+0

입니다. @Michael 당신은 아마도' foreach'를 찾아서 찾고있는 부동산이 그곳에 있는지 살펴보십시오. –

+0

다시 감사 Pekka! 너는 내 하루를 보냈다! – Michael

0

while 루프에 제한을 하드 코딩하는 대신 항목 목록을 반복 해보십시오.

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