Search code examples
phpsimplexmlsoap-clientebay-api

PHP simplexml_load_ is not returning objects from eBay API


I obtained the XML directly from the eBay API and it is valid. Originally I used CURL to call the SOAP API and the results are returned correctly as XML, but I need it in simplexml in order to loop through the results

I tried:

$results = ////get data via api
print $results = ////prints out all text and when viewed through 'view source' shows properly structured XML
$xml = simple_xml_load_string($results);
print_r($xml);

returns the below and nothing else:

SimpleXMLElement Object ( )

I then tried the same as the above, I just first saved the xml file locally and used

$xml = simple_xml_load_file('results.xml');
print_r($xml);

and I got the same result. Why is the data not loading?

Edit: I put in the following code and it just returns unable to load XML file

if( ! $xml = simplexml_load_string($results) ) 
    { 
            echo 'unable to load XML file'; 
        foreach(libxml_get_errors() as $error) {
            echo $error->message , '(',  $error->file , ':' ,  $error->line , ')<br>';
        }
    } 
    else 
    { 
        echo 'XML file loaded successfully'; 
    } 

As requested, here is the actual XML. Note that the element 'item' is actually repeated 100 times with different values.

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soapenv:Body>
  <GetSellerListResponse xmlns="urn:ebay:apis:eBLBaseComponents">
   <Timestamp>2014-03-05T14:55:27.562Z</Timestamp>
   <Ack>Success</Ack>
   <Version>863</Version>
   <Build>E863_CORE_APISELLING_16708011_R1</Build>
   <ItemArray>
    <Item>
     <ItemID>123456789</ItemID>
     <SellingStatus>
      <CurrentPrice currencyID="USD">99.99</CurrentPrice>
      <ListingStatus>Completed</ListingStatus>
     </SellingStatus>
     <Title>test ebay title</Title>
     <Variations>
      <Variation>
       <SKU>xxxxxxxxx</SKU>
       <StartPrice currencyID="USD">99.99</StartPrice>
       <Quantity>1/Quantity>
       <VariationSpecifics>
        <NameValueList>
         <Name>Color</Name>
         <Value>red</Value>
        </NameValueList>
        <NameValueList>
         <Name>size</Name>
         <Value>Large</Value>
        </NameValueList>
       </VariationSpecifics>
       <SellingStatus>
        <QuantitySold>0</QuantitySold>
       </SellingStatus>
      </Variation>
     </Variations>
    </Item>
    </ItemArray>
  </GetSellerListResponse>
 </soapenv:Body>
</soapenv:Envelope>

Solution

  • In order to determine why SimpleXML is not loading there are a couple of options.

    code:

    try {
      $oXml = new SimpleXMLElement($sXml);
    } catch(Exception $e) {
      print_r($e->getMessage());
    }
    

    Update

    It's spitting out tons of errors on my end, using the aforementioned checks, so I'm not sure why your PHP isn't showing the same. Anyway I found the problem. One of the tags is badly formatted. <Quantity>1/Quantity> should be <Quantity>1</Quantity>. Then there is another problem in that you are only providing a snippet of the Soap body.

    If you pull a chunk of the XML from the Soap body and fix the broken tag it will work. Or if you add the missing tags you can parse the whole Soap body with SimpleXML.

    $sXml =
    '<?xml version="1.0" encoding="UTF-8"?>
      <GetSellerListResponse xmlns="urn:ebay:apis:eBLBaseComponents">
       <Timestamp>2014-03-05T14:55:27.562Z</Timestamp>
       <Ack>Success</Ack>
       <Version>863</Version>
       <Build>E863_CORE_APISELLING_16708011_R1</Build>
       <ItemArray>
        <Item>
         <ItemID>123456789</ItemID>
         <SellingStatus>
          <CurrentPrice currencyID="USD">99.99</CurrentPrice>
          <ListingStatus>Completed</ListingStatus>
         </SellingStatus>
         <Title>test ebay title</Title>
         <Variations>
          <Variation>
           <SKU>xxxxxxxxx</SKU>
           <StartPrice currencyID="USD">99.99</StartPrice>
           <Quantity>1</Quantity>
           <VariationSpecifics>
            <NameValueList>
             <Name>Color</Name>
             <Value>red</Value>
            </NameValueList>
            <NameValueList>
             <Name>size</Name>
             <Value>Large</Value>
            </NameValueList>
           </VariationSpecifics>
           <SellingStatus>
            <QuantitySold>0</QuantitySold>
           </SellingStatus>
          </Variation>
         </Variations>
        </Item>
        </ItemArray>
      </GetSellerListResponse>';
    
    error_reporting(E_ERROR);
    libxml_use_internal_errors(true);
    try {
        $oXml = new SimpleXMLElement($sXml, null, false, "http://schemas.xmlsoap.org/soap/envelope/");
    } catch(Exception $e) {
        foreach(libxml_get_errors() as $error)
            var_dump($error);
    }
    
    var_dump((string)$oXml->children('urn:ebay:apis:eBLBaseComponents'));
    

    I'd suggest just using SoapClient and saving yourself the headache though.