I am using Laravel to create an app for the Linnworks API. I have created my own AJAX requests in PHP that receive the data from javascript.
This is a working request.
Linnworks Example
inventoryItemId=28c6226d-08de-4eb5-9bf1-f6089972cf18&fieldName=0&fieldValue=sample string 1&locationId=e10f6f45-d4ad-4128-8b58-6ff84b23199a
Javascript Request to PHP
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 Request
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());
}
This works and will update the stock levels of the item, however I need to update multiple items at the same time. I have found that Stock /SetStockLevel meets my requirements, however I can not format the request correctly and keep getting an error.
Linnworks Example
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
}
]