Stuart Stuart - 2 months ago 17
JSON Question

API Request, & Nested Request, Merge Results

I am making my first API call, which with Linnworks is rather slow and will only get worse as my inventory grows, so I would like to save the output to work with offline.

As I understand it, I make the first call to Linnworks for the Inventory;

$inventoryItems = Inventory::GetInventoryItems($views[0], $locationIds, 0, 10, $authorization->Token, "https://api.linnworks.net/");


Then based on the response of that I can then make the request for the Item Image(s);

$images = Inventory::GetInventoryItemImages($item->Id, $authorization->Token, "https://api.linnworks.net/");


I would like the Images to be added to the array from the first request $inventoryItems, how do I go about doing this?

Here is what I have so far;

$authorization = json_decode(Factory::GetResponse("Auth/AuthorizeByApplication", "applicationId=asddas&applicationSecret=asdasd&token=asdasdasd", "", "https://api.linnworks.net/"));
$views = Inventory::GetInventoryViews($authorization->Token, "https://api.linnworks.net/");
file_put_contents("/tmp/views.json",json_encode($views));
$locations = Inventory::GetStockLocations($authorization->Token, "https://api.linnworks.net/");
file_put_contents("/tmp/locations.json",json_encode($locations));
$locationIds = array();
foreach($locations as $location){ $locationIds[] = $locations[0]->StockLocationId; }
$inventoryItems = Inventory::GetInventoryItems($views[0], $locationIds, 0, 10, $authorization->Token, "https://api.linnworks.net/");
foreach($inventoryItems->Items as $item){
$images = Inventory::GetInventoryItemImages($item->Id, $authorization->Token, "https://api.linnworks.net/");
foreach($images as $image){
// Add Image to $inventoryItems ??
$imageSource = $image->Source;
}
}
// Save Complete Results Set
file_put_contents("/tmp/inventoryItems.json",json_encode($inventoryItems));


Part of $InventoryItems output;

stdClass Object
(
[Items] => Array
(
[0] => stdClass Object
(
[website] => stdClass Object
(
[LinksCount] => 1
[Templates] => Array
(
)

[Changes] => Array
(
)

[ContainsChanges] =>
)

)

[Id] => fe44bdcc-899f-47ff-958a-0285ed5e9936
[Title] => Product Title
[Category] => 00000000-0000-0000-0000-000000000000
[Image] =>
)


$images part output;

Array
(
[0] => stdClass Object
(
[pkRowId] => sdfsdfsdsddsf
[Source] => http://images.linnlive.com/werwerwerwerwerwe/tumbnail_sdfsdfsdf.jpg
[IsMain] => 1
[StockItemId] => fe44bdcc-899f-47ff-958a-0285ed5e9936
)

[1] => stdClass Object
(
[pkRowId] => asdasdasddsa
[Source] => http://images.linnlive.com/werwerwerwerwerwe/tumbnail_sdfsdfsdf.jpg
[IsMain] =>
[StockItemId] => fe44bdcc-899f-47ff-958a-0285ed5e9936
)

)
Array
(
[0] => stdClass Object
(
[pkRowId] => weerewrwerewerewrew
[Source] => http://images.linnlive.com/werwerwerwerwerwe/tumbnail_sdfsdfsdf.jpg
[IsMain] => 1
[StockItemId] => fb9f4983-5391-4320-b745-03eb4f48640f
)

)

Answer
foreach($inventoryItems->Items as &$item){
 $images = Inventory::GetInventoryItemImages($item->Id, $authorization->Token, "https://api.linnworks.net/");
     $item->images =[];

 foreach($images as $image){
     $imageSource = $image->Source;
     $item->images[] = $imageSource;
 }
}

$item is you object so it's pass by reference by default but if it's not working then you can use &$item in foreach statement.

Comments