1

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
        )

)
duncan
  • 31,401
  • 13
  • 78
  • 99
Stuart
  • 690
  • 2
  • 9
  • 26

1 Answers1

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

Stuart
  • 690
  • 2
  • 9
  • 26
siddhesh
  • 598
  • 6
  • 19
  • I was so close, Thank you @siddhest. – Stuart Sep 10 '16 at 10:04
  • It's my plessure but have a look at this also http://stackoverflow.com/questions/3307409/php-pass-by-reference-in-foreach – siddhesh Sep 10 '16 at 10:19
  • Half the problem with these things is know what you want to achieve but not knowing the question to ask, as you rightly pointed out in the first place. – Stuart Sep 10 '16 at 10:22