首页 > 解决方案 > 在 laravel 8 中将 2 个 JSON api 结果合并为一个数组

问题描述

控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;

class HomeController extends Controller
{

    private string $api_key;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->api_key = config('flickr.API_KEY');
    }


    /**
     * @throws RequestException
     */
    public function index()
    {
        $collection = array_merge($this->sizeImages(), $this->getContent());

        \Debugbar::info($collection);
        return view('welcome', compact('collection'));
    }


    /**
     * @return mixed
     * @throws RequestException
     */
    private function getContent()
    {
        $url = "https://www.flickr.com/services/rest/?method=flickr.photos.getPopular&api_key={$this->api_key}&user_id=23187053%40N04&extras=description%2Cowner_name%2Cviews&per_page=5&format=json&nojsoncallback=1";
        $response = Http::get($url);
        $responseJson = $response->json();

        if ($response->successful()) {
            foreach ($responseJson['photos']['photo'] as $key => $value) return $value;
        }
        return $response->throw();
    }


    /**
     * @return mixed
     * @throws RequestException
     */
    private function sizeImages()
    {
        $imageSizes = "https://www.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key={$this->api_key}&photo_id={$this->getContent()['id']}&format=json&nojsoncallback=1";
        $response = Http::get($imageSizes);
        $responseJson = $response->json();

        if ($response->successful()) {
            foreach ($responseJson['sizes']['size'] as $key => $value) return $value;
        }
        return $response->throw();
    }
}

路线

Route::get('/', [HomeController::class, 'index'])->name('home');

看法


            @if (isset($collection))
                @foreach ($collection as $key => $item)
               {{$item['source'}}
                @endforeach
            @endif

vardumping$collection控制器索引函数内部的变量给了我一个数组

array:18 [▼
  "label" => "Square"
  "width" => 75
  "height" => 75
  "source" => "https://live.staticflickr.com/5146/5570747940_6c83ca4520_s.jpg"
  "url" => "https://www.flickr.com/photos/23187053@N04/5570747940/sizes/sq/"
  "media" => "photo"
  "id" => "5570747940"
  "owner" => "23187053@N04"
  "secret" => "6c83ca4520"
  "server" => "5146"
  "farm" => 6
  "title" => "Two times cute."
  "ispublic" => 1
  "isfriend" => 0
  "isfamily" => 0
  "description" => array:1 [▶]
  "ownername" => "Cajaflez"
  "views" => "8109"
]

但是 var 在我的视图中转储$collection变量给了我 1 个数组,其余的都丢失了元素。

array:18 [▶]
Square
75
75
https://live.staticflickr.com/5146/5570747940_6c83ca4520_s.jpg
https://www.flickr.com/photos/23187053@N04/5570747940/sizes/sq/
photo
5570747940
23187053@N04
6c83ca4520
5146
6
times cute.
1
0
0
array:1 [▶]
Cajaflez8109

因此,尝试这样做{{$item['source'}}会产生 Illegal sting offset 错误。在我看来 foreach 循环。

我将如何解决这个问题?

标签: phplaravel

解决方案


您的视图中有语法错误,它应该是:

 @if (isset($collection))
    @foreach ($collection as $key => $item)
         {{ $item['source'] }}
    @endforeach
 @endif

推荐阅读