我创建了一个函数,使用 Inventory 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 Explorer,它将起作用。
{
"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;
}
反馈评论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 选择API:库存API 选择 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
响应体 空
eBay API 文档中的错误代码 2004
错误 ID:2004
错误类别:请求
消息:无效请求
长消息:请求有错误。如需帮助,请参阅此 API 的文档。
域:访问
HTTP 状态代码:400
我希望这有帮助。但我认为这与我之前写的完全一样。 我不知道错误可能出在哪里。在这种情况下,eBay 文档的效率并不高。 当我使用 Ebay 文档中的示例 JSON 时,这是同样的错误。所以我认为传输有问题。也许使用 CURLOPT_POST 发送数据的长度并不理想。但我已经以同样的方式完成了所有其他 API 调用。令牌、位置、列表本身、发布以及更新 API 调用都是以完全相同的方式实现的。
我已经开始工作了。 我必须按照“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;
}