php - 在 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 循环。
我将如何解决这个问题?
解决方案
您的视图中有语法错误,它应该是:
@if (isset($collection))
@foreach ($collection as $key => $item)
{{ $item['source'] }}
@endforeach
@endif
推荐阅读
- java - 通过 URLClassLoader 加载 .class 文件 - NoClassDefFoundError
- c# - 在 NModbus SlaveDataStore 中存储浮点数据类型 - InputRegisters
- javascript - 如何允许在 Highcharts 中垂直和水平滚动?
- mysql - 为什么我无法在我的 Laravel 应用程序中搜索短词?
- rest - 使用 Rally API 创建用户故事时如何传递所有者属性?
- javascript - 如何将对象转换为嵌套对象
- java - 如何从 mongodb 文档中的数组中检索数据
- css - 如何覆盖媒体查询中的所有类?
- python - 如何让 Scikit-learn TfidfVectorizer 不对文本进行预处理?
- python - 属性错误:类型对象“StartPage”没有属性“Username_Text”