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

时间:2016-09-10 08:31:24

标签: php arrays json

我正在进行我的第一次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 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
        )

)

1 个答案:

答案 0 :(得分:1)

$item

&$item是你的对象,所以默认情况下它是通过引用传递的,但如果它不起作用,那么你可以在foreach语句中使用LoadModule rewrite_module libexec/apache2/mod_rewrite.so