Search code examples
phpxmldomebay-api

Ebay api GetSellerList, Parsing response XML


I am using the ebay trading api to get a sellers stock which is currently listed. I am using the call GetSellerList.I am having trouble parsing the xml which I would then insert into there website shop.

This is the xml request.

<GetSellerListRequest xmlns='urn:ebay:apis:eBLBaseComponents'>

<UserID>".$user_id."</UserID>

<DetailLevel>ReturnAll</DetailLevel>
<ErrorLanguage>RFC 3066</ErrorLanguage>
<WarningLevel>Low</WarningLevel>
<Version>".$compat_level."</Version>

<RequesterCredentials>
    <eBayAuthToken>".$auth_token."</eBayAuthToken>
</RequesterCredentials>

<StartTimeFrom>2012-06-12T23:35:27.000Z</StartTimeFrom>
<StartTimeTo>2012-08-30T23:35:27.000Z</StartTimeTo>

<Pagination>
    <EntriesPerPage>200</EntriesPerPage>
</Pagination>

<OutputSelector>ItemArray.Item.Title</OutputSelector>
<OutputSelector>ItemArray.Item.Description</OutputSelector>
<OutputSelector>ItemArray.Item.BuyItNowPrice</OutputSelector>
<OutputSelector>ItemArray.Item.Quantity</OutputSelector>

</GetSellerListRequest>

I am not the best with php, I am still learning so i have looked through w3schools and php docs and found nothing. I have been using this (off of ebay tuts) to try and get the values of the xml tags by using getElementsByTagName.

$dom = new DOMDocument();
$dom->loadXML($response);

$titles = $dom->getElementsByTagName('Title')->length > 0 ? $dom->getElementsByTagName('Title')->item(0)->nodeValue : ''; 

Now i was hoping that i would be able to create an array with this then use foreach to insert them into the db but when i use this it only gets the value of the first 'Title' tag

Im sure there is a way to create an array with all values of 'Title' in it. All help is appreciated.


Solution

  • This would be easier to answer if you posted the response XML (just the relevant portion) rather than the request.

    The code you have will only grab the first item - specifically this part:

    $dom->getElementsByTagName('Title')->item(0)->nodeValue
    

    Rather, you'll want to loop through all the Title elements and extract their nodeValue. This is a starting point:

    $dom = new DOMDocument();
    $dom->loadXML($response);
    $title_nodes = $dom->getElementsByTagName('Title');
    
    $titles = array();
    
    foreach ($title_nodes as $node) {
        $titles[] = $node->nodeValue;
    }