11

我是 eBay API 的新手,目前正在使用 PHP 进行开发,我已经设法使用 GetItem 将基于项目 ID 的订单详细信息导入我的网站数据库。但我现在要做的是将用户帐户链接到我的网站并将他们的列表导入我的数据库。我已经将我用于 GetItem 的代码(如下)放在了:

首先:让我的用户从我的网站重定向到 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;
            }
        }
    }
4

3 回答 3

14

在 eBay 上阅读了很多关于它的 API 的糟糕文档并变得疯狂之后!我自己动手做了一个关于 API 的分步指南,并想出了一个方法来做到这一点。我将尝试尽可能简单地解释。(使用 PHP)

我们要做什么:

  1. 创建应用程序
  2. 从 eBay 为我们的用户获取会话 ID
  3. 使用会话 ID 连接到 eBay
  4. 用户授予对我们应用程序的访问权限以链接到他的用户帐户(使用会话 ID)
  5. 生成的用户令牌
  6. 我们的网站收到用户令牌以供将来使用(访问 eBay 上的用户数据)

首先 ,您需要两个 PHP 文件:keys.php 和 eBaySession.php,它们位于 eBay 的 PHP SDK 中,该 SDK 位于 eBay 的开发者网站文档中。(https://www.x.com/developers/ebay/documentation-tools/sdks

其次 ,您将在一个新的 PHP 文件中包含这两个文件,该文件也将包含用户界面。

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

第四 ,您将使用您的开发者帐户为您的应用程序获取沙盒和生产密钥。然后您将生成一个沙盒用户并获取一个用户令牌。(通过我的帐户页面

在 eBay 的开发者网站上找到自己可能有点困难,但您最终会找到窍门的。

第五 你将在keys.php文件中插入你的应用程序的DEV、APP、CERT和UserToken(生产和沙盒模式)

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

第七 您现在将 RuName 作为新参数插入到您的 keys.php 文件中:

$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 页面将使用来自 eBay 的会话 ID,$verb = 'GetSessionID';因此当我们单击“链接您的 Ebay 帐户”按钮时,用户将被发送到此 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 URL 上,因为 eBay 仅通过安全连接 (SSL) 接受这些功能。否则,您将难以运行此代码。

我最终会通过收到反馈来改进这个答案。我知道这可能会让你有点困惑,但我希望我能在时间之前把它变成一个更好的答案。如果您需要,我还在问题中介绍了 eBay API 的 GetItem 函数。

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

于 2013-01-04T20:34:14.113 回答
0

您不需要使用 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。

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

于 2013-07-15T01:57:59.390 回答
0

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

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

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

于 2016-05-26T08:07:54.413 回答