我从imdb中获取数据,当我试图在刀片视图中显示数据时,我面临很多错误。
可能是因为我不确定从api中得到的响应是什么。
请与我一起,并提前感谢您抽出时间阅读我的问题。
下面是来自api的一些可用的示例响应:
{
"d": [
{
"i": {
"height": 1500,
"imageUrl": "https://m.media-amazon.com/images/M/MV5BYTRiNDQwYzAtMzVlZS00NTI5LWJjYjUtMzkwNTUzMWMxZTllXkEyXkFqcGdeQXVyNDIzMzcwNjc@._V1_.jpg",
"width": 1102
},
"id": "tt0944947",
"l": "Game of Thrones",
"q": "TV series",
"rank": 36,
"s": "Emilia Clarke, Peter Dinklage",
"v": [
{
"i": {
"height": 720,
"imageUrl": "https://m.media-amazon.com/images/M/MV5BZTg4YzdjNTctNDg5Mi00ZmU1LTkzOWEtNmMyNDBjZjNhNTJiXkEyXkFqcGdeQXRyYW5zY29kZS13b3JrZmxvdw@@._V1_.jpg",
"width": 1280
},
"id": "vi59490329",
"l": "Official Series Trailer",
"s": "3:19"
},
{
"i": {
"height": 1080,
"imageUrl": "https://m.media-amazon.com/images/M/MV5BMTljMTZmNDUtNTEzNy00NDgyLTk2N2QtOTI3MGQyNWE0MTI5XkEyXkFqcGdeQWplZmZscA@@._V1_.jpg",
"width": 1920
},
"id": "vi1097842713",
"l": "The 8 Most Surprising Moments From \"Game of Thrones\" to Rewatch",
"s": "3:39"
},
{
"i": {
"height": 720,
"imageUrl": "https://m.media-amazon.com/images/M/MV5BMTg0ODM4NTc3OV5BMl5BanBnXkFtZTgwODAwODE1OTE@._V1_.jpg",
"width": 1280
},
],
"q": "game of thr",
"v": 1
}我尝试使用几种方式在刀片视图中显示数据,我尝试的最新方法是: blade.php:
@foreach ($data as $item)
{{$item['d']}}
@endforeach我得到这个作为回应:
{“数据”:{“i”:{“高度”:4096,"imageUrl":"https://m.media-amazon.com/images/M/MV5BMTg4NDA1OTA5NF5BMl5BanBnXkFtZTgwMDQ2MDM5ODE@..jpg","width":2764},"id":"tt2582782","l":"Hell还是高水“,”Q“:”特写“,”qid“:”电影“,”排名“:1332,"s":"Chris,Ben”,"y":2016},{“i”:{“高度”:755,"imageUrl":"https://m.media-amazon.com/images/M/MV5BMjM5ODQ5Nzc3OF5BMl5BanBnXkFtZTgwOTQzMzM4NjE@..jpg","width":509}“
我的控制器的功能是:
public function api(Request $request)
{
$userInput = $request->input();
$response = Http::withHeaders(
[
"x-rapidapi-host"=> "xxxxxxxxx",
"x-rapidapi-key"=> "xxxxxxxxxxx",
]
)->get("https://imdb8.p.rapidapi.com/auto-complete?q=",$userInput)->json();
return json_encode(array('data'=>$response));
}我也累了:
return view('view',['data'=>$response]);但却有不同的错误,比如:
非法偏移类型。
并尝试了其他方法,但也没有成功。
我错过了什么??请帮帮忙。
发布于 2022-09-15 09:47:35
如果这是api的响应,
{“数据”:{“i”:{“高度”:4096,"imageUrl":"https://m.media-amazon.com/images/M/MV5BMTg4NDA1OTA5NF5BMl5BanBnXkFtZTgwMDQ2MDM5ODE@.V1.jpg","width":2764},"id":"tt2582782","l":"Hell还是高水“,”Q“:”特写“,”qid“:”电影“,”排名“:1332,"s":"Chris,Ben”,"y":2016},{“i”:{“高度”:755,"imageUrl":"https://m.media-amazon.com/images/M/MV5BMjM5ODQ5Nzc3OF5BMl5BanBnXkFtZTgwOTQzMzM4NjE@.V1.jpg","width":509}“
那就不需要json_encode兄弟了。
$data = $response
return view('view_path',compact('data'));发布于 2022-09-15 10:06:10
尝尝这个。
公共函数api(Request $request) { $userInput = $request->input();
$response = Http::withHeaders(
[
"x-rapidapi-host"=> "xxxxxxxxx",
"x-rapidapi-key"=> "xxxxxxxxxxx",
]
)->get("https://imdb8.p.rapidapi.com/auto-complete?q=",$userInput);
return response->json(array('data'=>$response->body()));
}发布于 2022-09-15 10:37:13
这是一个很好的演示代码,但是您应该这样做。
这不是解决方案的一部分(忽略它),但我想展示一下我是如何测试它的。我已经将您的响应数据放到了我的test.json文件中。
$response = file_get_contents(public_path('json/test.json'));
$object = json_decode($response);
$array = collect($object)->toArray(); // Not needed if you get an array type in $object溶液
foreach ($array as $data) {
$data = collect($data)->toArray(); // Not needed if you get an array type in $data
foreach ($data as $items) {
$items = collect($items)->toArray(); // Not needed if you get an array type in $items
foreach ($items as $value) {
$value = collect($value)->toArray(); // Not needed if you get an array type in $value
$image = collect($value["i"])->toArray(); // Not needed if you get an array type in $value["i"]
$imageUrl = $image["imageUrl"];
$rank = $value["rank"];
}
}
}https://stackoverflow.com/questions/73728835
复制相似问题