Search code examples
phpxmlnodesebay-api

Parsing eBay Trading API with XML and PHP


I have the authentication working and it's pulling in the correct data, but its bringing in the whole HTML doc but just as code and not rendering it properly.

Closest thing on here I can seem to find is this answer on here: Ebay api GetSellerList, Parsing response XML Yet solutions still don't work.

Parsing the XML for eBay and I think I have to use the Nodes but have never done this before & is quite confusing. Tried a bunch of stuff and am going to keep trying until I can get this working. Thanks for the efforts! [hr] This is a screenshot of the response I'm getting from this code:

https://i.sstatic.net/iG5I4.jpg

Red line shows a series of information including time of API call, Failure/Success boolean & Version of API (987). Yellow lines show where the HTML starts & ends and there is another further down the page.

Also I know I have a doctype and html at the top of my document. Even if I took it out from doctype to the final last HTML hard coded section of "With a User Tocken ID we can import user data to our website." still doesn't work and gives the same HTML hardcode.

Link to pastebin if it's easier to read there: http://pastebin.com/LYJHpLnK

    <?php require_once('keys.php') ?>
    <?php require_once('eBaySession.php') ?>
    <?php session_start(); ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <HTML>
    <HEAD>
    <META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <TITLE>Get eBay User Items (Result)</TITLE>
    </HEAD>
    <BODY>
        <h2>Testing eBay Connection Plugin</h2>
        <h3>Receiving User Tocken</h3>
        <h4>With a User Tocken ID we can import user data to our website.</h4>

        <?php


                //SiteID must also be set in the Request's XML
                //SiteID = 0  (US) - UK = 3, Canada = 2, Australia = 15, ....
                //SiteID Indicates the eBay site to associate the call with
                $siteID = 0;
                //the call being made:
                $verb = 'FetchToken';

                ///Build the request Xml string
                $requestXmlBody = '<?xml version="1.0" encoding="utf-8" ?>';
                $requestXmlBody .= '<FetchTokenRequest xmlns="urn:ebay:apis:eBLBaseComponents">';
                $requestXmlBody .= '<SessionID>'.$_SESSION["eBaySession"].'</SessionID>';
                $requestXmlBody .= '</FetchTokenRequest>';


                //Create a new eBay session with all details pulled in from included keys.php
                $session = new eBaySession($userToken, $devID, $appID, $certID, $serverUrl, $compatabilityLevel, $siteID, $verb);

                //send the request and get response
                $responseXml = $session->sendHttpRequest($requestXmlBody);
                if(stristr($responseXml, 'HTTP 404') || $responseXml == '')
                    die('<P>Error sending request');

                //Xml string is parsed and creates a DOM Document object
                $responseDoc = new DomDocument();
                $responseDoc->loadXML($responseXml);


                //get any error nodes
                $errors = $responseDoc->getElementsByTagName('Errors');

                //if there are error nodes
                if($errors->length > 0)
                {
                    echo '<P><B>eBay returned the following error(s):</B>';
                    //display each error
                    //Get error code, ShortMesaage and LongMessage
                    $code = $errors->item(0)->getElementsByTagName('ErrorCode');
                    $shortMsg = $errors->item(0)->getElementsByTagName('ShortMessage');
                    $longMsg = $errors->item(0)->getElementsByTagName('LongMessage');
                    //Display code and shortmessage
                    echo '<P>', $code->item(0)->nodeValue, ' : ', str_replace(">", "&gt;", str_replace("<", "&lt;", $shortMsg->item(0)->nodeValue));
                    //if there is a long message (ie ErrorLevel=1), display it
                    echo '<BR/>User Session ID: '.$_COOKIE["eBaySession"].'';
                    if(count($longMsg) > 0)
                        echo '<BR>', str_replace(">", "&gt;", str_replace("<", "&lt;", $longMsg->item(0)->nodeValue));

                }

                else //no errors
                {
                    //get the nodes needed
                    $eBayAuthTokenNode = $responseDoc->getElementsByTagName('eBayAuthToken');

                    //Display the details
                    //echo '<BR/>User Session ID: '.$_SESSION["eBaySession"].'';
                    //echo '<BR/><BR/>User Token: '.$eBayAuthTokenNode->item(0)->nodeValue.'';
                    $verb = 'GetSellerList';
                    $username = $_GET["username"];
                    $CreateTimeFrom = gmdate("Y-m-d\TH:i:s",time()); //current time
                    $CreateTimeTo = date('Y-m-d', strtotime("+3 months", strtotime($CreateTimeFrom)));

                    $requestXmlBody = '<?xml version="1.0" encoding="utf-8" ?>';
                    $requestXmlBody .= '<GetSellerListRequest xmlns="urn:ebay:apis:eBLBaseComponents">';
                    $requestXmlBody .= '<UserID>'.$username.'</UserID>';
                    $requestXmlBody .= '<DetailLevel>ReturnAll</DetailLevel>';
                    $requestXmlBody .= '<ErrorLanguage>RFC 3066</ErrorLanguage>';
                    $requestXmlBody .= '<WarningLevel>Low</WarningLevel>';
                    $requestXmlBody .= '<Version>987</Version>';
                    $requestXmlBody .= '<RequesterCredentials><eBayAuthToken>'.$eBayAuthTokenNode->item(0)->nodeValue.'</eBayAuthToken></RequesterCredentials>';
                    $requestXmlBody .= '<StartTimeFrom>'.$CreateTimeFrom.'</StartTimeFrom>';
                    $requestXmlBody .= '<StartTimeTo>'.$CreateTimeTo.'</StartTimeTo>';
                    $requestXmlBody .= '<EndTimeFrom>'.$CreateTimeFrom.'</EndTimeFrom>';
                    $requestXmlBody .= '<EndTimeTo>'.$CreateTimeTo.'</EndTimeTo>';

                    $requestXmlBody .= '<Pagination><EntriesPerPage>200</EntriesPerPage></Pagination>';
                    $requestXmlBody .= '<OutputSelector>ItemArray.Item.Title</OutputSelector>';
                    $requestXmlBody .= '<OutputSelector>ItemArray.Item.Description</OutputSelector>';
                    $requestXmlBody .= '<OutputSelector>ItemArray.Item.BuyItNowPrice</OutputSelector>';
                    $requestXmlBody .= '<OutputSelector>ItemArray.Item.Quantity</OutputSelector>';
                    $requestXmlBody .= '</GetSellerListRequest>';







                   //Create a new eBay session with all details pulled in from included keys.php
                $session = new eBaySession($userToken, $devID, $appID, $certID, $serverUrl, $compatabilityLevel, $siteID, $verb);

                //send the request and get response
                $responseXml = $session->sendHttpRequest($requestXmlBody);
                if(stristr($responseXml, 'HTTP 404') || $responseXml == '')
                    die('<P>Error sending request');

                //Xml string is parsed and creates a DOM Document object
                $responseDoc = new DomDocument();
                $responseDoc->loadXML($responseXml);
                $html = $responseDoc->saveHTML();
                //echo $html;




               echo $responseXml;

                }


                //SiteID = 0  (US) - UK = 3, Canada = 2, Australia = 15, ....
    //SiteID Indicates the eBay site to associate the call with
    $siteID = 0;
    //the call being made:
    $verb = 'GetSellerList';
    //Time with respect to GMT
    //by default retreive orders in last 30 minutes
    $CreateTimeFrom = gmdate("Y-m-d\TH:i:s",time()-1800); //current time minus 30 minutes
    $CreateTimeTo = gmdate("Y-m-d\TH:i:s");
    //If you want to hard code From and To timings, Follow the below format in "GMT".
    //$CreateTimeFrom = YYYY-MM-DDTHH:MM:SS; //GMT
    //$CreateTimeTo = YYYY-MM-DDTHH:MM:SS; //GMT
    ///Build the request Xml string


    //by default retreive orders in last 30 minutes
    $CreateTimeFrom = gmdate("Y-m-d\TH:i:s",time()-1800); //current time minus 30 minutes
    $CreateTimeTo = gmdate("Y-m-d\TH:i:s");


     //send the request and get response
                $responseXml = $session->sendHttpRequest($requestXmlBody);
                if(stristr($responseXml, 'HTTP 404') || $responseXml == '')
                    die('<P>Error sending request');

                //Xml string is parsed and creates a DOM Document object
                $responseDoc = new DomDocument();
                $responseDoc->loadXML($responseXml);

                $title_nodes = $responseDoc->getElementsByTagName('Title');
    $titles = array();
    foreach ($title_nodes as $node) {
        $titles[] = $node->nodeValue;
    }

        ?>

        </BODY>
        </HTML>

Solution

  • If anyone still needs an answer to this, it was simply using the PHP decode($responseXml) and it printed it out perfectly.