提问人:Timothy 提问时间:2/2/2022 最后编辑:Brian Tompsett - 汤莱恩Timothy 更新时间:7/24/2023 访问量:916
在 laravel 中从外部 API 搜索特定数据
Search particular data from external API in laravel
问:
想要使用 laravel 从外部 API 获取数据
我是Laravel的新手,我想按名称从外部API(如下所述)搜索数据。
这是用于搜索的边栏选项卡视图:
<form method="get" action="{{route('search')}}">
{{ csrf_field() }}
<input type="text" name="search" placeholder="search meals">
<button type="submit">search</button>
</form>
路线
Route::get('/search', 'App\Http\Controllers\searchController@search')->name('search');
控制器功能
public function search(Request $request)
{
$request->input('search');
$meal = Http::get('www.themealdb.com/api/json/v1/1/search.php?s=${search}')->json();
dd($meal);
}
这是我想从中按名称(strMeal)获取数据的API:
{
"meals": [
{
"idMeal": "52771",
"strMeal": "Spicy Arrabiata Penne",
"strDrinkAlternate": null,
"strCategory": "Vegetarian",
"strArea": "Italian",
"strInstructions": "Bring a large pot of water to a boil. Add kosher salt to the boiling water, then add the pasta. Cook according to the package instructions, about 9 minutes.\r\nIn a large skillet over medium-high heat, add the olive oil and heat until the oil starts to shimmer. Add the garlic and cook, stirring, until fragrant, 1 to 2 minutes. Add the chopped tomatoes, red chile flakes, Italian seasoning and salt and pepper to taste. Bring to a boil and cook for 5 minutes. Remove from the heat and add the chopped basil.\r\nDrain the pasta and add it to the sauce. Garnish with Parmigiano-Reggiano flakes and more basil and serve warm.",
"strMealThumb": "https://www.themealdb.com/images/media/meals/ustsqw1468250014.jpg",
"strTags": "Pasta,Curry",
"strYoutube": "https://www.youtube.com/watch?v=1IszT_guI08",
"strIngredient1": "penne rigate",
"strIngredient2": "olive oil",
"strIngredient3": "garlic",
"strIngredient4": "chopped tomatoes",
"strIngredient5": "red chile flakes",
"strIngredient6": "italian seasoning",
"strIngredient7": "basil",
"strIngredient8": "Parmigiano-Reggiano",
"strIngredient9": "",
"strIngredient10": "",
"strIngredient11": "",
"strIngredient12": "",
"strIngredient13": "",
"strIngredient14": "",
"strIngredient15": "",
"strIngredient16": null,
"strIngredient17": null,
"strIngredient18": null,
"strIngredient19": null,
"strIngredient20": null,
"strMeasure1": "1 pound",
"strMeasure2": "1/4 cup",
"strMeasure3": "3 cloves",
"strMeasure4": "1 tin ",
"strMeasure5": "1/2 teaspoon",
"strMeasure6": "1/2 teaspoon",
"strMeasure7": "6 leaves",
"strMeasure8": "spinkling",
"strMeasure9": "",
"strMeasure10": "",
"strMeasure11": "",
"strMeasure12": "",
"strMeasure13": "",
"strMeasure14": "",
"strMeasure15": "",
"strMeasure16": null,
"strMeasure17": null,
"strMeasure18": null,
"strMeasure19": null,
"strMeasure20": null,
"strSource": null,
"strImageSource": null,
"strCreativeCommonsConfirmed": null,
"dateModified": null
}
]
}
结果
转储后,它显示空数组
答:
0赞
Anand
2/2/2022
#1
我通过GuzzleHTTP尝试了这个
刀片模板搜索
<form action="{{ route('search.submit') }}" method="POST">
@csrf
<div class="mb-3">
<label for="">Search</label>
<input type="text" class="form-control" name="search">
</div>
<div class="mb-3">
<button type="submit" class="btn btn-primary">Search</button>
</div>
</form>
然后用于 Post Request 控制器功能
use GuzzleHttp\Client;
public function search(Request $request)
{
$url = "https://www.themealdb.com/api/json/v1/1/search.php?s=";
$search_query = $url . $request->s;
$client = new Client();
$response = $client->request('GET', $search_query);
echo $response->getBody();
}
评论
0赞
Timothy
2/2/2022
它无法正常工作,我想根据我搜索到的单词获得结果。.
评论