phpjsoncurlebay-api

Error eBay Inventory API createOrReplaceInventoryItem


i created a funciton to create an inventory item on eBay using the Inventory API. The script generates a json from a database and sends it to the ebay api url. When i execute the function on my site i get an error as result with no further informations.

Array
(
    [errors] => Array
        (
            [0] => Array
                (
                    [errorId] => 2004
                    [domain] => ACCESS
                    [category] => REQUEST
                    [message] => Invalid request
                    [longMessage] => The request has errors. For help, see the documentation for this API.
                )

        )

)

When i send the same json with API Explorer to the same api url i get a successful 204 result.

I hope you guys have some hints or a solution to get this thing working. Thanks in Advance.

The JSON thats generated from my script. You can copy the json an insert it in the API Explorer an it will work.

{
    "product":{
        "title":"    Original  ",
        "brand":"Bosch",
        "mpn":"A444",
        "aspects":{
            "Produktart": ["Abschleppkabel"],
            "Im Lieferumfang enthalten":["Abschleppkette"],
            "Besonderheiten":["Geringe Dehnung"],
            "Material":["Kohlenstoffstahl"],
            "Zulässige Tragfähigkeit":["100"],
            "Geeignet für":["Wohnmobil"],
            "Gütesiegel & Kennzeichnungen":["TÜV Rheinland\/GS"],
            "Durchmesser":["78 Zoll"],
            "Herstellungsland und -region":["Deutschland"],
            "Maximale Belastbarkeit":["151 kg"]
        },
        "description":"TEST",
        "imageUrls":["https:\/\/www.eccaro.de\/uploads\/img\/"],
        "condition":"USED_EXCELLENT",
        "packageWeightAndSize":{
            "dimensions":{
                "height":"200",
                "length":"300",
                "width":"100",
                "unit":"CENTIMETER"
            },
            "weight":{
                "value":"69",
                "unit":"GRAM"
            }
        },
        "availability":{
            "shipToLocationAvailability":{
                "quantity":2
            }
        }
    }
}

My Function


function export_part($token_data,$ebay_db,$aspects,$aspect_string,$artbez,$her_name,$hnr,$galurl,$hoehe_part,$laenge_part,$breite_part,$gewicht,$menge,$bestandseinheit){
    $headers = array
    (
        'Authorization: Bearer '. $token_data['TokenValue'],
        'Accept:application/json',
        'Content-type: application/json',
        'Content-Language:de-DE',
    );

    $url = 'https://api.ebay.com/sell/inventory/v1/inventory_item/'.$bestandseinheit;
    echo $url;
    
    //Build JSON
    $test->product->title = $artbez;
    $test->product->brand = "Bosch";
    $test->product->mpn = $hnr;
    $aspect_string;
    foreach($aspects as $key => $aspect){
        if($aspect == 'NOTSET'){
        }else{
            $test->product->aspects->$key[0] = $aspect;
        }                
    }
    $test->product->description = "TEST";
    $test->product->imageUrls[0] = $galurl;
    $test->product->condition = "USED_EXCELLENT";
    $test->product->packageWeightAndSize->dimensions->height = $hoehe_part;
    $test->product->packageWeightAndSize->dimensions->length = $laenge_part;
    $test->product->packageWeightAndSize->dimensions->width = $breite_part;
    $test->product->packageWeightAndSize->dimensions->unit = "CENTIMETER";
    $test->product->packageWeightAndSize->weight->value = $gewicht;
    $test->product->packageWeightAndSize->weight->unit = "GRAM";
    $test->product->availability->shipToLocationAvailability->quantity = $menge;
    

    $test = json_encode($test,JSON_UNESCAPED_UNICODE);

    echo "<pre>";
    echo $test;
    echo "</pre>";

    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 3);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $test);
    $data = curl_exec($ch);
    curl_close($ch);
    $response = json_decode($data, true);

    
    print_r('<pre>');
    print_r($response);
    print_r('</pre>');
    

    return $response;   
}

Feedback to Comment ADyson

I use the API Explorer with the following.

Web Service URI (endpoint) https://api.ebay.com/sell/inventory/v1/inventory_item/12345678

HTTP Headers

Authorization:Bearer 
Accept:application/json
Content-Type:application/json
Content-Language:de-DE

Request Body

Exact the JSON from the top

Site ID : 77 Select API: Inventory API Select an API Call: createOrReplaceInventoryItem Advanced

Call Response Status: 204 No ContentTime: 1670 ms

Response Headers

x-ebay-c-request-id:ri=OQyFByKejb%2Fh,rci=0a2960442f6ba7ba
rlogid:t6pitnmsgwj70%3D9vjdpitnmsgwj70*iflek%28rbpv6775-18cf427b8db-0x178
x-ebay-c-version:1.0.0
content-language:de-DE
x-ebay-client-tls-version:TLSv1.3
x-ebay-request-id:18cf427b-8db0-a49e-b4a6-19abf6872fae!inventory_item_PUT!slcslrinvapi26-jcjbh-tess0040.stratus.slc.ebay.com!r1slrinvapi26[]
set-cookie:ebay=%5Esbf%3D%23%5E;Domain=.ebay.com;Path=/; Secure
content-encoding:gzip
cache-control:private
pragma:no-cache
content-type:application/json
date:Wed, 10 Jan 2024 16:14:52 GMT
server:ebay-proxy-server
x-envoy-upstream-service-time:304
x-ebay-pop-id:UFES2-LVSAZ01-api
connection:close

Response Body empty


Error Code 2004 in eBay API Documentation

Error ID: 2004

Error Category: REQUEST

Message: Invalid request

Long Message: Request has errors. For help, see the documentation for this API.

Domain: ACCESS

HTTP Status Code: 400

I hope this helps. But i think thats exactly the same what i wrote i before. I have no idea where the error could be. In this case the eBay documentation is not that productive. When i use a sample JSON from the Ebay Documentation, it is the same error. So i think the transmission is faulty. Maybe the sending with the CURLOPT_POST is for the length of the data not ideal. But i have done all other API Calls on the same way. The Token, the location, the listing itself, the publishing and also the update API calls are realized the excatly same way.


Solution

  • I got the things to work. I had to send the curl per "PUT" so i added the line

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 
    

    instead of

    curl_setopt($ch, CURLOPT_POST, true);
    

    The whole code looks like this ( I added another request to check if my inventory item is successfull transmitted

        if($token->success == true) {
        export_part($token->db_token,$ebay_db,$aspects,$aspect_string,$artbez,$her_name,$hnr,$galurl,$hoehe_part,$laenge_part,$breite_part,$gewicht,$menge,$bestandseinheit);
        getpart($token->db_token,$bestandseinheit);
    }
    
    function export_part($token_data,$ebay_db,$aspects,$aspect_string,$artbez,$her_name,$hnr,$galurl,$hoehe_part,$laenge_part,$breite_part,$gewicht,$menge,$bestandseinheit){
    
        $url = 'https://api.ebay.com/sell/inventory/v1/inventory_item/'.$bestandseinheit;
        echo $url;
        
        //Build JSON
        $test->product->title = $artbez;
        $test->product->brand = "Bosch";
        $test->products->mpn = $hnr;
        $aspect_string;
        foreach($aspects as $key => $aspect){
            if($aspect == 'NOTSET'){
            }else{
                $test->product->aspects->$key[0] = $aspect;
            }                
        }
        $test->product->description = "TEST";
        $test->product->imageUrls[0] = $galurl;
        $test->product->condition = "USED_EXCELLENT";
        $test->product->packageWeightAndSize->dimensions->height = $hoehe_part;
        $test->product->packageWeightAndSize->dimensions->length = $laenge_part;
        $test->product->packageWeightAndSize->dimensions->width = $breite_part;
        $test->product->packageWeightAndSize->dimensions->unit = "CENTIMETER";
        $test->product->packageWeightAndSize->weight->value = $gewicht;
        $test->product->packageWeightAndSize->weight->unit = "GRAM";
        $test->product->availability->shipToLocationAvailability->quantity = $menge;
        
    
        $test = json_encode($test,JSON_UNESCAPED_UNICODE);
    
        echo "<pre>";
        echo $test;
        echo "</pre>";
    
            $headers = array
        (
            'Authorization: Bearer '. $token_data['TokenValue'],
            'Accept:application/json',
            'Content-type: application/json',
            'Content-Language:de-DE',
        );
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 3);
        curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 
        curl_setopt($ch, CURLOPT_POSTFIELDS, $test);
        $data = curl_exec($ch);
        $response = json_decode($data, true);
        curl_close($ch);
    
        
        print_r('<pre>');
        print_r($response);
        print_r('</pre>');
        
    
        return $response;   
    }
    
    function getpart($token_data,$bestandseinheit){
        $url = 'https://api.ebay.com/sell/inventory/v1/inventory_item/'.$bestandseinheit;
        echo $url;
        
        $headers = array
        (
            'Authorization: Bearer '. $token_data['TokenValue'],
            'Accept:application/json',
            'Content-type: application/json',
            'Content-Language:de-DE',
        );
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 3);
        curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
        curl_setopt($ch, CURLOPT_POST, false); 
        //curl_setopt($ch, CURLOPT_POSTFIELDS, $test);
        $data = curl_exec($ch);
        curl_close($ch);
        $response = json_decode($data, true);
    
        
        print_r('<pre>');
        print_r($response);
        print_r('</pre>');
        
    
        return $response;  
    
    }