newPHPDev
newPHPDev

Reputation: 47

eBay API Single Item Call, Item Specifics

I am having a tough time trying to parse through this. I am more of a Java developer so I implemented this using arrays.

How can I go about that in PHP?

<ItemSpecifics>
   <NameValueList>
      <Name>Professional Grader</Name>
      <Value>Professional Sports (PSA)</Value>
   </NameValueList>
   <NameValueList>
      <Name>Year</Name>
      <Value>2012</Value>
   </NameValueList>
   <NameValueList>
      <Name>Card Attributes</Name>
       <Value>Rookie</Value>
      </NameValueList>
</ItemSpecifics>

This is the response from eBay in XML

I am setting the specifcsName and and specificsValue like so

if ($item->ItemSpecifics != NULL ) {
    if ($item->ItemSpecifics->NameValueList != NULL ) {
      if ($item->ItemSpecifics->NameValueList->Name != NULL ) {
        $itemSpecificName = $item->ItemSpecifics->NameValueList->Name;
      } else {
        $itemSpecificName = NULL;
      }
    } else {
      $itemSpecificName =  NULL;
    }
  } else {
    $itemSpecificName = NULL;
  }

  if ($item->ItemSpecifics != NULL ) {
    if ($item->ItemSpecifics->NameValueList != NULL ) {
      if ($item->ItemSpecifics->NameValueList->Value != NULL ) {
        $itemSpecificValue = $item->ItemSpecifics->NameValueList->Value;
      } else {
        $itemSpecificValue = NULL;
      }
    } else {
      $itemSpecificValue =  NULL;
    }
  } else {
    $itemSpecificValue = NULL;
  }

Here is where I am trying to display it.

<?php
          if ($itemSpecificName != NULL || $itemSpecificValue != NULL) {
            foreach ($item->ItemSpecifics as $key => $value) {
              foreach ($item->ItemSpecifics->NameValueList as $key => $value) {
                foreach ($itemSpecificName as $key => $value) {
                  ?> <li> <?php echo($value);?></li><?php
                  foreach ($itemSpecificValue as $key => $value) {
                    ?> <span style="margin-left:25px;"> <?php echo($value);?></span><br><?php
                  }
                }
              }
            }


          } else {
            ?> <i>Not specified</i> <?php
          }
          ?>

The above code is returning the follow:

 Professional Grader     
 Professional Sports (PSA)  
 Professional Grader
 Professional Sports (PSA)  
 Professional Grader     
 Professional Sports (PSA)

It's not parsing EACH NameValueList.

Upvotes: 3

Views: 315

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57121

You have too many layers in your code and I'm assuming a few things (use of SimpleXML and that there is another layer of data above what you show). But a simple version of the output would be like...

$data = <<< XML
<OuterLayer>
<ItemSpecifics>
   <NameValueList>
      <Name>Professional Grader</Name>
      <Value>Professional Sports (PSA)</Value>
   </NameValueList>
   <NameValueList>
      <Name>Year</Name>
      <Value>2012</Value>
   </NameValueList>
   <NameValueList>
      <Name>Card Attributes</Name>
       <Value>Rookie</Value>
      </NameValueList>
</ItemSpecifics>
</OuterLayer>
XML;
$item = simplexml_load_string($data);
foreach ($item->ItemSpecifics as $value) {
    foreach ($value->NameValueList as $value) {
        echo $value->Name."/".$value->Value.PHP_EOL;
    }
}

One thing I noticed with your loops is that as you can see from the above code, the output of the one loop acts as the start point of the next loop. This gives the nesting effect of accessing the various levels of data.

You will have to wrap the output in whatever HTML your after, but hope this is enough to get you started.

Upvotes: 1

Related Questions