我已经阅读了许多文档,并尝试了在Internet上找到的解决方案,并且正在努力使用GetSingleItem请求从eBay Shopping API中检索所需的数据。我相信我已经根据文档中的示例正确设置了标头和xml请求,但是我无法确定如何实际发送请求和检索xml响应。有人可以帮我下一步吗?
我实际上如何发送请求并检索响应?
我的标头和xml请求如下:
// Create headers
$headers = array
(
'X-EBAY-API-APP-ID: ' . $app_id,
'X-EBAY-API-SITE-ID: ' . $site_id,
'X-EBAY-API-CALL-NAME: ' . $call_name,
'X-EBAY-API-VERSION: ' . $version,
'X-EBAY-API-REQUEST-ENCODING: ' . $encoding,
);
// Generate XML request
$xml_request = '<?xml version="1.0" encoding="utf-8"?>\n';
$xml_request .= '<GetSingleItemRequest xmlns="urn:ebay:apis:eBLBaseComponents">\n';
$xml_request .= '<ItemID>283814879195</ItemID>\n';
$xml_request .= '<IncludeSelector>Details</IncludeSelector>\n';
$xml_request .= '</GetSingleItemRequest>';
下一步是什么?
如果有帮助,这是到目前为止我已经尝试过的完整代码,但是当我尝试使用simplexml_load_string时出现“无法加载”的情况。为了安全起见,我也删除了我的app_id:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
function getItem($itemID) {
$endpoint = 'https://open.api.ebay.com/shopping';
$app_id = 'XXXXXXXXXXX';
$site_id = '3';
$call_name = 'GetSingleItem';
$version = '863';
$encoding = 'xml';
// Create headers to send with CURL request.
$headers = array
(
'X-EBAY-API-APP-ID: ' . $app_id,
'X-EBAY-API-SITE-ID: ' . $site_id,
'X-EBAY-API-CALL-NAME: ' . $call_name,
'X-EBAY-API-VERSION: ' . $version,
'X-EBAY-API-REQUEST-ENCODING: ' . $encoding,
);
// Generate XML request
$xml_request = '<?xml version="1.0" encoding="utf-8"?>\n';
$xml_request .= '<GetSingleItemRequest xmlns="urn:ebay:apis:eBLBaseComponents">\n';
$xml_request .= '<ItemID>283814879195</ItemID>\n';
$xml_request .= '<IncludeSelector>Details</IncludeSelector>\n';
$xml_request .= '</GetSingleItemRequest>';
$session = curl_init($endpoint); // create a curl session
curl_setopt($session, CURLOPT_POST, true); // POST request type
curl_setopt($session, CURLOPT_HTTPHEADER, $headers); // set headers using $headers array
curl_setopt($session, CURLOPT_POSTFIELDS, $xml_request); // set the body of the POST
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // return values as a string, not to std out
$responsexml = curl_exec($session); // send the request
curl_close($session); // close the session
return $responsexml; // returns a string
}
$itemID = '283814879195';
$resp = simplexml_load_string(getItem($itemID)) or die("Failed to load");
print($resp->Item->ItemID);
?>
答案 0 :(得分:0)
我遇到了同样的问题。 完成后,我将在我的Github上公开发布我的项目,所以就到这里。
请注意, ItemID 正在传递到函数中。如果打算对itemID进行硬编码,则可以忽略第一行代码中的GET。
$ItemID = $_GET['itemid'];
itemDetails($ItemID);
然后
function itemDetails($ItemID) {
$url = 'https://open.api.ebay.com/shopping';
$app_id = '[ADD YOUR APP ID]';
$site_id = '0';
$call_name = 'GetSingleItem';
$version = '863';
$encoding = 'xml';
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
$xml.= "<GetSingleItemRequest xmlns=\"urn:ebay:apis:eBLBaseComponents\">";
$xml .= "<ItemID>".$ItemID."</ItemID>";
$xml .= "</GetSingleItemRequest>";
$headers = array(
'X-EBAY-API-APP-ID:'.$app_id,
'X-EBAY-API-SITE-ID:'.$site_id,
'X-EBAY-API-CALL-NAME:'.$call_name,
'X-EBAY-API-VERSION:'.$version,
'X-EBAY-API-REQUEST-ENCODING:'.$encoding,
'Content-type:text/xml;charset=utf-8'
);
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, $url);
curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);
curl_setopt($connection, CURLOPT_POST, 1);
curl_setopt($connection, CURLOPT_POSTFIELDS, $xml);
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($connection);
echo $result;
curl_close($connection);
}
希望这会有所帮助!