我创建了一个函数来使用库存API在eBay上创建库存项目. 该脚本从数据库生成一个json,并将其发送到ebay api url. 当我在我的网站上执行该函数时,我得到一个错误,结果没有进一步的信息.

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.
                )

        )

)

当我使用API Explorer将相同的json发送到相同的API url时,我得到了一个成功的204结果.

我希望你们有一些提示或解决方案,让这件事运作起来.提前谢谢你.

从我的脚本生成的JSON.您可以复制JSON并将其插入到API资源管理器中,这样它就可以工作了.

{
    "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
            }
        }
    }
}

我的功能


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

反馈到 comments ADYSON

我将API Explorer与以下内容一起使用.

Web服务URI(终结点) https://api.ebay.com/sell/inventory/v1/inventory_item/12345678

HTTP标头

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

请求主体

从头开始精确定位JSON

站点ID:77 Select 接口:盘点接口 Select 一个API调用:createOrReplaceInventoryItem Advanced

呼叫响应 状态:204无内容时间:1670毫秒

响应标头

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

响应体 空的


易趣API文档中的错误代码2004

错误ID:2004

错误类别:请求

消息:请求无效

长消息:请求有错误.有关帮助,请参阅此接口的文档.

域:访问

HTTP状态代码:400

我希望这能帮到你.但我想这和我以前写的一模一样. 我不知道错误可能出在哪里.在这种情况下,eBay文档并不是很有成效. 当我使用eBay文档中的样例JSON时,它是相同的错误.所以我认为变速箱有故障.可能使用CURLOPT_POST发送的数据长度不理想.但我已经以相同的方式完成了所有其他API调用.令牌、位置、列表本身、发布以及更新API调用都以完全相同的方式实现.

推荐答案

我把事情都做好了. 我不得不把curl 按"PUT"发送,所以我添加了这行

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

而不是

curl_setopt($ch, CURLOPT_POST, true);

整个代码看起来像这样(我添加了另一个请求来判断我的库存项目是否成功传输

    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;  

}

Php相关问答推荐

CodeIgniter AJAX表格提交中的CSRF token 再生问题

如何优化-PHP 7.3.14+Laravel 6

PHP:从数组中获取唯一的数字组合

如何实现对数据库表中多列的唯一约束?

在WooCommerce上克隆订单时使用`add_product()`添加产品元数据

PHP文件上载问题-";Move_Uploaded_Files";未按预期工作

即使在WooCommerce购物车中添加了两次产品,也要将所有项目设置为空白行

在PHP项目中安装OpenAI PHP SDK时出现问题

PHP按另一个二维数组的排序顺序对二维数组进行排序

将购物车按钮重定向到Checkout-Wooccommerce

如何在自定义邮箱内容中获取WooCommerce订单项目的详细信息?

根据小计向 WooCommerce Checkout 添加佣金

使用 foreach php 将记录正确分配给正确的父级

WordPress 分页无法正常工作

PHP - 计算非数值数组

在 Symfony 测试中何时使用 TestCase 而不是 KernelTestCase

PHP删除具有相同键的项目,除了具有最低值的项目

如何用 php ML 解决这个问题?

如何在 Process facade 中转义特殊字符?

如何在 WooCommerce 中显示每个自定义选项卡的内容