【发布时间】:2021-11-10 00:35:52
【问题描述】:
eBay 正在对其 API 进行更改,并在年底关闭查找 API。我在我的 WordPress 网站上使用了一个非常简单的应用程序,它根据我硬编码的特定查询显示产品。我可以使用 Browse API 复制我的页面,但我真的在努力处理 Oauth 部分。据我了解,浏览获取请求仅需要应用程序访问令牌(而不是用户访问令牌)。我只是仍在努力解决所有这些问题。我正在尝试添加一个生成身份验证令牌的函数,但它不起作用,我认为我没有正确调用该函数......所以在这里寻求一些帮助。最重要的是——我可以这样做吗?我是否正确输入了变量以及如何准确调用 Oauth 函数?
<?php
/* Template Name: XXXX */
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
function getOAuthCreds() {
$endpoint = 'https://api.ebay.com/identity/v1/oauth2/token';
$request = "grant_type=client_credentials";
$request .= "scope=https://api.ebay.com/oauth/api_scope";
$session = curl_init($endpoint);
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_POSTFIELDS, $request);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$headers = [
'Content-Type: application/json',
'Authorization = Bearer CODE HERE// I'm using the auth code generated from the application access token (not sure if thats' right?
];
curl_setopt($session, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($session);
curl_close($session);
return $response;
}
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.ebay.com/buy/browse/v1/item_summary/search?q=iphone&sort=-",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Content-Type:application/json",
"Authorization:" getOAuthCreds(),///am i calling this correctly?
"X-EBAY-C-MARKETPLACE-ID:EBAY_US",
"X-EBAY-C-ENDUSERCTX:affiliateCampaignId=xx,affiliateReferenceId=xx",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
//print_r($response);
if ($err) {
if ($debug) echo "cURL Error #:" . $err;
else echo "Oops, something went wrong. Please try again later.";
} else {
//Create an array of objects from the JSON returned by the API
$jsondata = json_decode($response);
$resp = $jsondata->itemSummaries;
//Create a simple grid style for the listings
$pageCSS = "<style>
.netflix-wrapper{
display:grid;
grid-template-columns: 200px 200px 200px;
}
.show-wrapper{padding:10px;}
</style>";
//Create the WordPress page content HTML
$pageHTML="<h2>test</h2>";
$pageHTML.="<div class='test-wrapper'>";
//Loop through the API results
foreach($resp as $item) {
//Put each show into an html structure
// Note: if your theme uses bootstrap use responsive classes here
$pageHTML.="<div class='show-wrapper'>";
//Not all items have a 'poster', so in that case use the img field
$pic = $item->image->imageUrl;
$itemID = $item->legacyItemId;
$link = 'https://www.ebay.com/itm/'.$itemID.'?mkrid=ss-0&siteid=0&mkcid=1&campid=ss&toolid=ss&mkevt=1&customId=ss';
$title = $item->title;
$price = $item->price->value;
$bids = $item->bidCount;
if(empty($bids)){
$bids = 0;
}
// For each SearchResultItem node, build a link and append it to $results
$results .= "<div class=\"item\"><div class=\"ui small image\"><a href=\"$link\" target=\"_blank\"><img height=\"200px\" width=\"130px\" src=\"$pic\"></a></div><div class=\"content\"><div class=\"header\"><a href=\"$link\" target=\"_blank\">$title</a></div><div class=\"meta\" style=\"margin-top:1.1em\"><span class=\"price\"><a href=\"$link\" target=\"_blank\"><button class=\"ui teal button\">watch watchers</button></a></span><div class=\"extra\"><button class=\"ui button\"><a href=\"$link\" target=\"_blank\"><b>Current Bids:</b> $bids </a></button></div><div class=\"extra\"><a href=\"$link\" target=\"_blank\"><button class=\"ui orange button\">Current Price: $$price</button></a></div><div class=\"description\"></div></div></div></div>";
//Show the image first to keep the top edge of the grid level
$pageHTML.="<img style='max-width:166px;float:left;' src='".$pic."' />";
$pageHTML.="<h3>".$item->title."</h3>";
// $pageHTML.="<span>added to netflix ".$showObj->titledate."</span>";
// $pageHTML.="<div style='float:left;'>".$showObj->synopsis."</div>";
$pageHTML.="</div>";
}
$pageHTML.="</div>";
}
?>
<?php get_header(); ?>
<!-- Build the HTML page with values from the call response -->
<html>
<head>
<div class="wrp cnt"><div class="spr"></div>
<section class="bSe fullWidth">
<article><div class="awr lnd">
<title>Most Watched <?php echo $query; ?> on eBay</title>
<style type="text/css">body { font-family: arial,sans-serif;} </style>
<link
rel="stylesheet"
href="//cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css"
/>
</head>
<body>
【问题讨论】:
-
"Authorization:" getOAuthCreds(),///am i calling this correctly?- 很可能:否。令牌端点可能不会返回“裸”令牌,而是包含令牌本身的 JSON 数据结构,以及其他信息,例如何时到期。 -
并且尝试在每次页面加载时请求一个新令牌(如果这是您目前正在做的事情?),也可能不是一个好主意。而且,如果您不需要将实际数据“新鲜”到一秒,那么可能也不会在每次页面加载时发出第二个请求......结果实际上应该缓存一段时间。
-
@CBroe 感谢您的回复。我确实弄清楚了,至少生成了令牌部分,并且您 100% 正确地认为返回的对象需要为令牌本身进行分离。就目前而言,我想我是在页面加载时请求一个令牌,这是我第一次这样做,在这种情况下似乎如此。数据不必是最新的,但有些更新......因为这些是 ebay 列表。您对在实际需要时请求令牌或缓存结果有什么建议吗?
-
关于缓存,看Transients。并获得一个新令牌,因为在您的一个 API 请求因令牌已过期而 失败 之后,获取一个新令牌可能就足够了。然后刷新令牌,再次发出请求。