I'm fetching data from the imdb api and when i try to display the data in a blade view I'm facing a lot of errors.
probably because I'm not sure what is the response I'm getting from the api.
please bare with me and thanks in advance for taking the time to read through my question.
so here are some of the available example responses from the 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
}
I have tried to display the data in my blade view using a couple of ways the latest way I'm trying is this, blade.php:
@foreach ($data as $item)
{{$item['d']}}
@endforeach
I'm getting this as a response:
{"data":{"d":[{"i":{"height":4096,"imageUrl":"https://m.media-amazon.com/images/M/MV5BMTg4NDA1OTA5NF5BMl5BanBnXkFtZTgwMDQ2MDM5ODE@.V1.jpg","width":2764},"id":"tt2582782","l":"Hell or High Water","q":"feature","qid":"movie","rank":1332,"s":"Chris Pine, Ben Foster","y":2016},{"i":{"height":755,"imageUrl":"https://m.media-amazon.com/images/M/MV5BMjM5ODQ5Nzc3OF5BMl5BanBnXkFtZTgwOTQzMzM4NjE@.V1.jpg","width":509}
the function in my Controller is this :
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));
}
I have also tired:
return view('view',['data'=>$response]);
but got different errors like:
Illegal offset type.
and tried other ways as well but also didn't succeed.
what I'm I missing?? please help.
Try this.
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);
return response->json(array('data'=>$response->body()));
}
If this is the response from api ,,,,
{"data":{"d":[{"i":{"height":4096,"imageUrl":"https://m.media-amazon.com/images/M/[email protected]","width":2764},"id":"tt2582782","l":"Hell or High Water","q":"feature","qid":"movie","rank":1332,"s":"Chris Pine, Ben Foster","y":2016},{"i":{"height":755,"imageUrl":"https://m.media-amazon.com/images/M/[email protected]","width":509}
then no need to json_encode buddy.
$data = $response
return view('view_path',compact('data'));
This is a just demonstration code but you should do something like this.
This is not part of your solution (ignore it) but I wanted to show how I tested it. I have put your response data in mytest.json
file.
$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
Solution
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"];
}
}
}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.