【问题标题】:Trouble Using eBay Shopping API with PHP (GetSingleItem Request)在 PHP 中使用 eBay 购物 API 时遇到问题(GetSingleItem 请求)
【发布时间】:2020-04-11 10:46:11
【问题描述】:

我已多次阅读文档并尝试了在互联网上找到的解决方案,但我正在努力使用 GetSingleItem 请求从 eBay 购物 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);

?>

【问题讨论】:

    标签: php xml http https ebay-api


    【解决方案1】:

    我遇到了同样的问题。 完成后,我将在我的 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);
    }
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多