eBay API下载用户列表(eBay API用户指南/分步指南)

时间:2012-12-31 17:41:06

标签: php xml ebay

我是eBay API的新手,目前正在使用PHP进行开发,我已设法使用GetItem将基于商品ID的订单详细信息导入我网站的数据库。但我现在要做的是将用户帐户链接到我的网站并将其列表导入我的数据库。我已经把我用于GetItem的代码(下面),但现在我卡住了,我不知道要使用什么,GetAccount,GetUser或GetSellerList:

首先:让我的用户从我的网站重定向到eBay,以授权我的应用访问他/她的列表。

第二名:将该商家信息(现在回声已经足够)导入我的网站。

这是我的GetItem代码:

     require_once('keys.php');
     require_once('eBaySession.php');

    if(isset($_POST['Id']))
    {
        //Get the ItemID inputted
        $id = $_POST['Id'];


        //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 = 101;
        //the call being made:
        $verb = 'GetItem';

        ///Build the request Xml string
        $requestXmlBody = '<?xml version="1.0" encoding="utf-8" ?>';
        $requestXmlBody .= '<GetItemRequest xmlns="urn:ebay:apis:eBLBaseComponents">';
        $requestXmlBody .= "<RequesterCredentials><eBayAuthToken>$userToken</eBayAuthToken></RequesterCredentials>";;
        $requestXmlBody .= "<ItemID>$id</ItemID>";
        $requestXmlBody .= '</GetItemRequest>';

        //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
            if(count($longMsg) > 0)
                echo '<BR>', str_replace(">", "&gt;", str_replace("<", "&lt;", $longMsg->item(0)->nodeValue));

        }

        else //no errors
        {
            //get the nodes needed
            $titleNode = $responseDoc->getElementsByTagName('Title');
            $primaryCategoryNode = $responseDoc->getElementsByTagName('PrimaryCategory');
            $categoryNode = $primaryCategoryNode->item(0)->getElementsByTagName('CategoryName');
            $listingDetailsNode = $responseDoc->getElementsByTagName('ListingDetails');
            $startedNode = $listingDetailsNode->item(0)->getElementsByTagName('StartTime');
            $endsNode = $listingDetailsNode->item(0)->getElementsByTagName('EndTime');

            $ShippingPackageDetailsNode = $responseDoc->getElementsByTagName('ShippingPackageDetails');
            if ($ShippingPackageDetailsNode->length > 0) {
                $packageDepthNode = $ShippingPackageDetailsNode->item(0)->getElementsByTagName('PackageDepth');
                $DepthUnit = $packageDepthNode->item(0)->getAttribute('unit');
                $packageLengthNode = $ShippingPackageDetailsNode->item(0)->getElementsByTagName('PackageLength');
                $LengthUnit = $packageLengthNode->item(0)->getAttribute('unit');
                $packageWidthNode = $ShippingPackageDetailsNode->item(0)->getElementsByTagName('PackageWidth');
                $WidthUnit = $packageWidthNode->item(0)->getAttribute('unit');
            }

            $sellingStatusNode = $responseDoc->getElementsByTagName('SellingStatus');
            $currentPriceNode = $sellingStatusNode->item(0)->getElementsByTagName('CurrentPrice');
            $currency = $currentPriceNode->item(0)->getAttribute('currencyID');
            $startPriceNode = $responseDoc->getElementsByTagName('StartPrice');
            $buyItNowPriceNode = $responseDoc->getElementsByTagName('BuyItNowPrice');
            $bidCountNode = $sellingStatusNode->item(0)->getElementsByTagName('BidCount');

            $sellerNode = $responseDoc->getElementsByTagName('Seller');

            //Display the details
            echo '<P><B>', $titleNode->item(0)->nodeValue, " ($id)</B>";
            echo '<BR>Category: ', $categoryNode->item(0)->nodeValue;
            echo '<BR>Started: ', $startedNode->item(0)->nodeValue;
            echo '<BR>Ends: ', $endsNode->item(0)->nodeValue;

            if ($ShippingPackageDetailsNode->length > 0) {
                echo "<BR>Package Length: ", $packageLengthNode->item(0)->nodeValue, ' '.$LengthUnit.'';
                echo "<BR>Package Width: ", $packageWidthNode->item(0)->nodeValue, ' '.$WidthUnit.'';
                echo "<BR>Package Depth: ", $packageDepthNode->item(0)->nodeValue, ' '.$DepthUnit.'';
            }

            echo "<P>Current Price: ", $currentPriceNode->item(0)->nodeValue, $currency;
            echo "<BR>Start Price: ", $startPriceNode->item(0)->nodeValue, $currency;
            echo "<BR>BuyItNow Price: ", $buyItNowPriceNode->item(0)->nodeValue, $currency;
            echo "<BR>Bid Count: ", $bidCountNode->item(0)->nodeValue;

            //Display seller detail if present
            if($sellerNode->length > 0)
            {
                echo '<P><B>Seller</B>';
                $userIDNode = $sellerNode->item(0)->getElementsByTagName('UserID');
                $scoreNode = $sellerNode->item(0)->getElementsByTagName('FeedbackScore');
                $regDateNode = $sellerNode->item(0)->getElementsByTagName('RegistrationDate');

                echo '<BR>UserID: ', $userIDNode->item(0)->nodeValue;
                echo '<BR>Feedback Score: ', $scoreNode->item(0)->nodeValue;
                echo '<BR>Registration Date: ', $regDateNode->item(0)->nodeValue;
            }
        }
    }

3 个答案:

答案 0 :(得分:14)

在eBay上阅读了很多关于它的API并且变得疯狂的不良文档!我亲自处理了问题,并逐步指导了API并找到了实现这一目标的方法。我将尝试尽可能简单地解释。 (使用PHP)

我们将采取的措施:

  1. 创建应用程序
  2. 从eBay
  3. 获取我们用户的会话ID
  4. 使用会话ID
  5. 连接到易趣
  6. 用户授予对我们的应用程序的访问权限以链接到他的用户帐户 (使用会话ID)
  7. 生成用户令牌
  8. 我们的网站收到用户令牌以供将来使用(访问用户数据) 在eBay上)
  9. <强>第一 您需要两个名为:keys.php和eBaySession.php的PHP文件,这些文件位于eBay的PHP SDK中,该SDK位于eBay的开发者网站Documentations中。 (的 https://www.x.com/developers/ebay/documentation-tools/sdks

    <强>第二 您将把这两个文件包含在一个新的PHP文件中,该文件也将保存用户界面。

    <强>第三 您将在eBay的开发者网站上创建一个帐户并创建一个新的应用程序。

    <强>四 您将使用开发人员帐户获取应用程序的沙箱和生产密钥。然后,您将生成沙箱用户并获取用户令牌。 (通过我的帐户页面

    在eBay的开发者网站上找到自己可能有点困难,但你最终会发现它的存在。

    <强>第五 您将在keys.php文件中插入应用程序的DEV,APP,CERT和UserToken(用于生产和沙盒模式)

    <强>第六 您需要一个RuName,它也位于我的帐户页面(管理您的RuNames )。

    <强>第七 现在,您将在keys.php文件中插入RuName作为新参数:

    $RuName = 'your RuName key';
    

    所以我们的 keys.php 将如下所示:

    <?php
        //show all errors - useful whilst developing
        error_reporting(E_ALL);
    
        // these keys can be obtained by registering at http://developer.ebay.com
    
        $production         = true;   // toggle to true if going against production
        $compatabilityLevel = 551;    // eBay API version
    
        if ($production) {
            $devID = 'production dev id';   // these prod keys are different from sandbox keys
            $appID = 'production app id';
            $certID = 'production cert id';
            $RuName = 'production RuName';
            //set the Server to use (Sandbox or Production)
            $serverUrl = 'https://api.ebay.com/ws/api.dll';      // server URL different for prod and sandbox
            //the token representing the eBay user to assign the call with
            $userToken = 'production user token';
        } else {
            // sandbox (test) environment
            $devID = 'sandbox dev id';   // these prod keys are different from sandbox keys
            $appID = 'sandbox app id';
            $certID = 'sandbox cert id';
            //set the Server to use (Sandbox or Production)
            $serverUrl = 'https://api.sandbox.ebay.com/ws/api.dll';
            // the token representing the eBay user to assign the call with
            // this token is a long string - don't insert new lines - different from prod token
            $userToken = 'sandbox user token';
        }
    
    
    ?>
    

    <强>八 现在我们将构建我们的第一个页面,为用户提供一些输出,如下所示:

    <?php require_once('keys.php') ?>
    <?php require_once('eBaySession.php') ?>
    <?php
    
            session_start();
            //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 = 'GetSessionID';
    
            ///Build the request Xml string
            $requestXmlBody = '<?xml version="1.0" encoding="utf-8" ?>';
            $requestXmlBody .= '<GetSessionIDRequest xmlns="urn:ebay:apis:eBLBaseComponents">';
            $requestXmlBody .= '<RuName>'.$RuName.'</RuName>';
            $requestXmlBody .= '</GetSessionIDRequest>';
    
            //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
                if(count($longMsg) > 0)
                    echo '<BR>', str_replace(">", "&gt;", str_replace("<", "&lt;", $longMsg->item(0)->nodeValue));
    
            }
    
            else //no errors
            {
                //get the nodes needed
                $sessionIDNode = $responseDoc->getElementsByTagName('SessionID');
                //Display the details
                $sessionID = $sessionIDNode->item(0)->nodeValue;
                $_SESSION['eBaySession'] = $sessionID;
    
            }
    ?>
    <!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</TITLE>
    </HEAD>
    <BODY>
    <FORM action="GetItem.php" method="post">
        <h2>Testing eBay Connection Plugin</h2>
        <h3>Linking User Account to our website</h3>
        <p>Session ID: <?php echo $_SESSION['eBaySession']; ?></p>
        <BR><a href="https://signin.ebay.com/ws/eBayISAPI.dll?SignIn&RuName=<?php echo $RuName; ?>&SessID=<?php echo $sessionID; ?>">Click Here To Link Your Ebay Account To Our Website</a>
    </FORM>
    </BODY>
    </HTML>
    

    这个新的PHP页面将使用$verb = 'GetSessionID';从eBay收到会话ID,因此当我们点击“链接您的易趣帐户”按钮时,用户将被发送到此URL:

    https://signin.ebay.com/ws/eBayISAPI.dll?SignIn&RuName=<?php echo $RuName; ?>&SessID=<?php echo $sessionID; ?>
    

    其中包含您的RuName和会话ID。

    <强>第九 用户将登录eBay,授予对您的应用程序的访问权限并发送回您的网站。现在,我们将使用前一部分中的相同会话ID来使用$verb = 'FetchToken';接收用户令牌(因为我们现在可以访问用户的帐户)。

    <?php require_once('keys.php') ?>
    <?php require_once('eBaySession.php') ?>
    <!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
    
                session_start();
                //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.'';
    
                }
        ?>
    
        </BODY>
        </HTML>
    

    然后你就可以获得访问权限和令牌。 但请确保您在HTTPS网址上托管此内容,因为eBay仅通过安全连接(SSL)接受这些功能。否则,您将无法运行此代码。

    我最终会通过收到反馈来改进这个答案。我知道这可能会让你感到困惑,但我希望我可以在时间上做出更好的答案。如果你需要,我还在问题中介绍了eBay API的GetItem函数。

    编辑:当然,您可以集成cUrl和XML请求。

答案 1 :(得分:1)

您不需要使用eBay的SDK。或者那些2 PHP包含您提供的文件。我像你一样疯了,我制作了自己的SDK文件,它实际上只是做了一些XML工作和cURL。我正在签订合同,所以我还不能共享我的文件,但它只有170行代码,你可以按照以下方式使用整个eBay API

$ebay = new Ebay();
$ebay->call("ReviseItem",array(
    "ItemID"=>"1234"
));

所以你应该在ebay上使用这个api测试工具 https://developer.ebay.com/DevZone/build-test/test-tool/default.aspx

然后你可以通过你想要的任何电话并阅读参数。它们很糟糕,但它并不比这更容易。

同样,我希望我可以分享我的代码,但我只是让你知道,如果你失败了,你只需要编写一些cURL和XML转换就可以“直接”使用不含SDK的API。 / p>

我为亚马逊MWS apis和google docs apis做了同样的事情。我希望能够尽快分享这一切

答案 2 :(得分:0)

@Hossein Jabbari解决方案有效。所以基本上你需要下载ebaysession.php文件并将其包含在你的应用程序中。它将为您处理所有curl / xml部分。

将所有应用详细信息插入keys.php文件中。然后,当你创建一个重定向url名称时,你的auth接受的URL应该是第二个具有fetchToken函数的php文件。由于您将会话ID从第一个PHP文件存储到会话中,因此检索它应该很容易。

然后,转到第一个PHP文件,单击登录URL。然后,将自己登录到生产或沙箱站点,一旦单击“接受”,您将被重定向到第二个PHP页面,然后您将能够看到您的令牌。