eBay api 搜索商品

eBay api search item

我想通过关键词搜索获取商品。

<?php
error_reporting(E_ALL);

require_once("includes/config.php");

// Construct the HTTP GET call
$apicall = "https://api.ebay.com/buy/browse/v1/item_summary/search?"; // 
URL to call
$apicall .= "q=GTR"; //search keyword
$apicall .= "limit=3"; 

// Create headers to send with CURL request.
$headers = array
(
'X-EBAY-API-APP-ID: ' . $sapp_id,
'X-EBAY-API-DEV-NAME: ' . $dev_id,
'X-EBAY-API-CERT-NAME: ' . $cert_id,
'X-EBAY-C-ENDUSERCTX: 5337984774'
);

// Send request to eBay and load response in $response
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, 
"https://api.ebay.com/buy/browse/v1/item_summary/search?");
curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);
curl_setopt($connection, CURLOPT_POST, 1);
curl_setopt($connection, CURLOPT_POSTFIELDS, $apicall);
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($connection);
curl_close($connection);
// Load the call and capture the document returned by eBay API
$resp = $response;

// Output response
echo("<pre>");

print_r($resp);
//To retrieve specific values of the array in PHP, you will need to use 
object operator
// $value = $resp->Item->ConvertedCurrentPrice;
// $value = $resp->Item->ConvertedCurrentPrice;
// print $xml->Product->Title;

echo("</pre>");

?>

我在 运行 时遇到了这个错误。

{
"errors": [
{
"errorId": 3001,
"domain": "ROUTING",
"category": "REQUEST",
"message": "Request Rejected",
"longMessage": "The Request has errors. For help, see the documentation for this API."
}
]
}

由于我是初学者,所以我不知道该怎么做。

我尝试了 xml 请求方法,它工作正常,但我想用 REST API 方法来完成。

我遵循这个文档 - Official eBay search api documentation

我想知道为什么会出现此错误以及实现此搜索查询的正确方法是什么?

未测试,因为我没有 eBay 开发者帐户,但以下内容可能会有所帮助。下面的代码将使用查询字符串作为 url 的一部分发出 GET 请求,正如 documentation 建议的那样

<?php

    error_reporting( E_ALL );

    require_once("includes/config.php");

    /* 
        you can download from https://curl.haxx.se/docs/caextract.html
    */
    $cacert='c:/wwwroot/cacert.pem'; # edit as appropriate to point to valid `cacert.pem` file
    $verbose=true;




    $endpoint = "https://api.ebay.com/buy/browse/v1/item_summary/search?";
    $args=array(
        'q'     =>  'GTR',
        'limit' =>  3
    );
    $url=sprintf( '%s%s', $endpoint, http_build_query( $args ) );

    /*
        for advanced debug information we create a stream
        and output to that during the request. At the end 
        of the request the stream is processed and we can
        view that data.
    */
    if( $verbose )$vbh = fopen('php://temp', 'w+');

    $headers = array(
        'X-EBAY-API-APP-ID: ' . $sapp_id,
        'X-EBAY-API-DEV-NAME: ' . $dev_id,
        'X-EBAY-API-CERT-NAME: ' . $cert_id,
        'X-EBAY-C-ENDUSERCTX: 5337984774'
    );

    $curl = curl_init();
    curl_setopt( $curl, CURLOPT_URL, $url );
    curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true );
    curl_setopt( $curl, CURLOPT_CAINFO, $cacert );
    curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
    curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt( $curl, CURLOPT_USERAGENT, 'curl' );

    if( $verbose ){
        curl_setopt( $curl, CURLOPT_VERBOSE, true );
        curl_setopt( $curl, CURLOPT_NOPROGRESS, true );
        curl_setopt( $curl, CURLOPT_STDERR, $vbh ); # the stream object
    }

    /* make the request and get info */
    $response = curl_exec( $curl );
    $info=(object)curl_getinfo( $curl );
    $errors=curl_error( $curl );

    /* store the verbose debug info */
    if( $verbose ){
        rewind( $vbh );
        $debug=stream_get_contents( $vbh );
        fclose( $vbh );
    }
    curl_close( $curl );




    if( $info->http_code==200 ){

        printf( '<pre>%s</pre>', print_r( $response, 1 ) );

    } else{

        printf( '<pre>%s</pre>', print_r( $verbose ? $debug : $info ,1 ) );
        printf( '<pre>%s</pre>', print_r( $errors, 1 ) );
    }
?>