Search code examples
phpebay-api

eBay api search item


I want to get items by keyword search.

<?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>");

?>

i got this error when i run.

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

Since i am beginner i don't know what to do.

i tried xml request method and it's work fine but i want to do it with REST API method.

i follow this docs - Official eBay search api documentation

i like to know why i am getting this error and what's the correct way to achieve this search query ?


Solution

  • Not tested as I don't have an eBay developer account but perhaps the following might help. The code below will issue a GET request with the querystring as part of the url as the documentation suggests

    <?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 ) );
        }
    ?>