我正在用IMDB api在APIS上训练laravel,我想以一种好的方式显示返回的响应吗?我是怎么做到的?例如



这是我在控制器中的功能

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);
}
}

我的路线是:

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

我得到的回复是:

"{"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} ◀

I don’我不想以一种好的方式显示结果,有关于如何的建议吗

简单的答案可以是使用json_decode((,它将json答案转换为PHP值:

json_decode($response)

但是Laravel并没有使用PHP curl函数,而是为您提供了Http客户端。

例如,您的查询可能看起来像:

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]);
}

相关内容

  • 没有找到相关文章

最新更新