API请求,&;嵌套请求,合并结果


API Request, & Nested Request, Merge Results

我正在进行我的第一个API调用,使用Linnworks的调用相当缓慢,而且只会随着库存的增长而变得更糟,所以我想保存输出以脱机使用。

据我所知,我第一次打电话给Linnworks索取库存;

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

然后,基于该响应,我可以提出对项目图像的请求;

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

我希望将第一个请求$inventoryItems中的图像添加到阵列中,我该如何执行此操作?

以下是我迄今为止所拥有的;

$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));

$InventoryItems输出的一部分;

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部分输出;

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
        )
)
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是您的对象,所以默认情况下它是通过引用传递的,但如果它不起作用,那么您可以在foreach语句中使用&$item