我是eBay API的新手,目前正在使用PHP开发,我已经设法使用GetItem将基于Item 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(">", ">", str_replace("<", "<", $shortMsg->item(0)->nodeValue));
//if there is a long message (ie ErrorLevel=1), display it
if(count($longMsg) > 0)
echo '<BR>', str_replace(">", ">", str_replace("<", "<", $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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Hos*_*ari 14
在eBay上阅读了很多关于它的API并且变得疯狂的不良文档!我亲自处理了问题,并逐步指导了API并找到了实现这一目标的方法.我将尝试尽可能简单地解释.(使用PHP)
我们将要做什么:
首先 您需要两个名为:keys.php和eBaySession.php的PHP文件,它们位于eBay的PHP SDK中,位于eBay的开发者网站Documentations中.(https://www.x.com/developers/ebay/documentation-tools/sdks)
第二步 您将这两个文件包含在一个新的PHP文件中,该文件也将保存用户界面.
第三, 您将在eBay的开发者网站上创建一个帐户并创建一个新的应用程序.
第四, 您将使用开发人员帐户获取应用程序的沙箱和生产密钥.然后,您将生成沙箱用户并获取用户令牌.(通过我的帐户页面)
在eBay的开发者网站上找到自己可能有点难,但你最终会发现它的悬念.
第五, 您将在keys.php文件中插入应用程序的DEV,APP,CERT和UserToken(用于生产和沙盒模式)
第六, 你需要一个RuName,它也位于我的帐户页面(管理您的RuName).
第七, 现在您将在keys.php文件中插入RuName作为新参数:
$RuName = 'your RuName key';
Run Code Online (Sandbox Code Playgroud)
所以我们的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';
}
?>
Run Code Online (Sandbox Code Playgroud)
八, 现在我们将构建我们的第一个页面,为用户提供一些输出,如下所示:
<?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(">", ">", str_replace("<", "<", $shortMsg->item(0)->nodeValue));
//if there is a long message (ie ErrorLevel=1), display it
if(count($longMsg) > 0)
echo '<BR>', str_replace(">", ">", str_replace("<", "<", $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>
Run Code Online (Sandbox Code Playgroud)
这个新的PHP页面将从eBay收到会话ID,$verb = 'GetSessionID';因此当我们点击"链接您的易趣帐户"按钮时,用户将被发送到以下URL:
https://signin.ebay.com/ws/eBayISAPI.dll?SignIn&RuName=<?php echo $RuName; ?>&SessID=<?php echo $sessionID; ?>
Run Code Online (Sandbox Code Playgroud)
其中包含您的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(">", ">", str_replace("<", "<", $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(">", ">", str_replace("<", "<", $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>
Run Code Online (Sandbox Code Playgroud)
在那里你可以获得访问权限和令牌.但请确保您在HTTPS URL上托管此内容,因为eBay仅通过安全连接(SSL)接受这些功能.否则,您将难以运行此代码.
我最终会通过收到反馈来改进这个答案.我知道这可能会让你感到困惑,但我希望我能在时间上把它变成更好的答案.如果您需要,我还在问题中介绍了eBay API的GetItem函数.
编辑:当然,您可以集成cUrl和XML请求.
| 归档时间: |
|
| 查看次数: |
8251 次 |
| 最近记录: |