我正在使用Laravel为Linnworks API创建一个应用程序。我已经在PHP中创建了自己的AJAX请求,该请求从javascript接收数据。
这是一个工作要求。
Linnworks示例
inventoryItemId=28c6226d-08de-4eb5-9bf1-f6089972cf18&fieldName=0&fieldValue=sample string 1&locationId=e10f6f45-d4ad-4128-8b58-6ff84b23199a
向PHP发送Javascript请求
var itemArray =
{
"inventoryItemId": "39692932-f8b0-42d7-9419-0500d6d79769",
"fieldName": "StockLevel",
"fieldValue": 6,
"locationId": "00000000-0000-0000-0000-000000000000"
};
var itemArrayStr = JSON.stringify(itemArray);
AjaxRequestNew({'dataArray': itemArrayStr, 'linnworksURL': 'Inventory/UpdateInventoryItemLevels'});
function AjaxRequestNew(ajaxData) {
$.ajax({
headers: {'X-CSRF-TOKEN': mytoken},
url: '/api-linnworks-request',
method: 'post',
data: ajaxData,
async: false,
complete: function (data) {
resp = data.responseText;
}
});
}
PHP AJAX请求
public function apiLinnworksRequest(Request $request){
$url ='https://eu1.linnworks.net//api/'.$request->linnworksURL;
$data = json_decode($request->dataArray);
$myvar = WebController::linnworksRequestNew($url, $data , $request);
}
public function linnworksRequestNew($url, $data , $request) {
$client = new Client();
// dd($data);
$res = $client->request(
'POST', $url, [
'headers' => [
'Authorization' => $request->session()->get('LinnReturnToken')
],
'form_params' => $data
]);
var_dump($res->getBody()->__toString());
}
这有效,并且将更新该项目的库存水平,但是我需要同时更新多个项目。我发现Stock / SetStockLevel符合我的要求,但是我无法正确格式化请求并不断出错。
Linnworks示例
stockLevels=[
{
"SKU": "sample string 1",
"LocationId": "2260b419-69dd-4586-b2b2-08db9591921c",
"Level": 3
},
{
"SKU": "sample string 1",
"LocationId": "2260b419-69dd-4586-b2b2-08db9591921c",
"Level": 3
}
]
最佳答案
感谢Linnworks的帮助和使用Postman的帮助,我发现请求的格式需要为以下格式。
的PHP
'stocklevels' =>
'[ { "SKU": "SUIT-4PC-[philip / paisley]-BLACK-02yrs", ]
"LocationId": "00000000-0000-0000-0000-000000000000",
"Level": 3 }, { "SKU": "SUIT-4PC-[philip / paisley]-BLACK-03yrs",
"LocationId": "00000000-0000-0000-0000-000000000000",
"Level": 3 } ]'
为了进行复制,我以以下格式发送了来自Javascript的数据:
JS
var itemArray =
[
{
"SKU": "SUIT-4PC-[philip / paisley]-BLACK-02yrs",
"LocationId": "00000000-0000-0000-0000-000000000000",
"Level": 6
},
{
"SKU": "SUIT-4PC-[philip / paisley]-BLACK-03yrs",
"LocationId": "00000000-0000-0000-0000-000000000000",
"Level": 6
}
];
var itemArrayStr = JSON.stringify(itemArray);
var dataRequest = {stocklevels: itemArrayStr};