I am training laravel on APIS , with IMDB api , and i want to display the returned response in a good way? how i do that ? for example

272 views Asked by At

that is my function in the controller

public function search (Request $search) {
    $curl = curl_init();
    $q=$search->movie;
    curl_setopt_array($curl, [
        CURLOPT_URL => "https://imdb8.p.rapidapi.com/auto-complete?q=$q",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_HTTPHEADER => [
            "X-RapidAPI-Host: imdb8.p.rapidapi.com",
            "X-RapidAPI-Key: xxxxxxxxxxxxxxxxxxxxxxxxx"
        ],
    ]);

    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        dd($response);
    }
}

my route is :

Route::get('/search', [HomeController::class,'search']);

and the response i got:

"{"d":[{"i":{"height":2048,"imageUrl":"https://m.media-amazon.com/images      /M/MV5BMTkzODYzNjYyNl5BMl5BanBnXkFtZTgwNjM0NjA5MjE@._V1_.jpg","width":1388},"id":"tt3722118","l":"God the Father","q":"feature","qid":"movie","rank":57803,"s":"Tom Benedict Knight, Amanda Fernando Stevens","y":2014},{"i":{"height":420,"imageUrl":"https://m.media-amazon.com/images/M/MV5BMTM2OTM3OTg1MF5BMl5BanBnXkFtZTcwMjg0OTkyMQ@@._V1_.jpg","width":300},"id":"tt0093103","l":"The Good Father","q":"feature","qid":"movie","rank":44657,"s":"Anthony Hopkins, Jim Broadbent","y":1985},{"i":{"height":900,"imageUrl":"https://m.media-amazon.com/images/M/MV5BOGYzODZhMmYtODI0Yy00ZTliLTgwMzYtN2Y3YjAxOTgxMDY0XkEyXkFqcGdeQXVyMTEzNTkzNzI2._V1_.jpg","width":654},"id":"tt11531324","l":"God Father","q":"feature","qid":"movie","rank":81523,"s":"Ananya, Ashwanth Ashokkumar","y":2020},{"i":{"height":600,"imageUrl":"https://m.media-amazon.com/images/M/MV5BZTk2ZGRjNTEtYWYwOS00M2ZjLWE2YjAtYjQ3OGQ5NjQ4MzJhXkEyXkFqcGdeQXVyNjU0NTI0Nw@@._V1_.jpg","width":450},"id":"tt15566744","l":"The Good Father: The Martin MacNeill Story","q":"TV movie","qid":"tvMovie","rank":22088,"s":"Tom Everett Scott, Anwen O'Driscoll","y":2021},{"i":{"height":2048,"imageUrl":"https://m.media-amazon.com/images/M/MV5BYmIxMjM2N2MtYjJiMC00NDNmLWExMDEtZjYyYjIyNjMzMDEwXkEyXkFqcGdeQXVyOTI3MzI4MzA@._V1_.jpg","width":1364},"id":"tt13130308","l":"Godfather","q":"feature","qid":"movie","rank":906,"s":"Chiranjeevi, Salman Khan","y":2022},{"i":{"height":2560,"imageUrl":"https://m.media-amazon.com/images/M/MV5BMjYzOTQ4NzItYTliMS00Nzc1LWEzNTctMDY5MGU5YWMxNmYxXkEyXkFqcGdeQXVyMTEzNDczNjY3._V1_.jpg","width":1792},"id":"tt13476378","l":"My God! Father","q":"feature","qid":"movie","rank":149478,"s":"Sammy Cowell, Chantavit Dhanasevi","y":2020},{"id":"tt15387126","l":"God Father","q":"feature","qid":"movie","rank":183411,"s":"Nadira, Sultan Rahi","y":1992},{"id":"tt15164290","l":"God Father","q":"feature","qid":"movie","rank":270374,"s":"Abhijit, Ashalatha","y":1996}],"q":"god%20gather","v":1} ◀

t want to show the result in a good way, any advice about how to

1

There are 1 answers

2
Jackh4l On

Short answer could be to use json_decode(), which will translate json answer into PHP values :

json_decode($response)

But instead of using PHP curl functions, Laravel provides Http client for what you're looking for.

For example your query could look like :

public function search(Request $request)
{
   $response = \Http
      ::withHeaders([
         'X-RapidAPI-Key' => 'xxxxxxxxxxxxxxxxxxx',
         'X-RapidAPI-Host' => 'imdb8.p.rapidapi.com',
      ])
      ->get('https://imdb8.p.rapidapi.com/auto-complete', ['q' => 'thrones']);

   $data = json_decode($response, true);
   dd($data);

   return response()->json(['data'=> $data]);
}