php - 使用 PHP 格式化 API Json 响应以在我的网站上很好地显示它
问题描述
所以我是使用 API 的新手,在搜索了一个包含“汉堡”一词的食谱后,我得到了这个结果。现在,我将如何在我的浏览器中很好地显示这个?
这是结果:
''' {"results":[{"id":492564,"title":"Falafel Burgers with Feta Cucumber Sauce","readyInMinutes":50,"servings":6,"sourceUrl":"https://www.cinnamonspiceandeverythingnice.com/falafel-burgers-with-feta-tzatziki-sauce/","openLicense":0,"image":"falafel-burgers-with-feta-tzatziki-sauce-492564.jpg"},{"id":246916,"title":"Bison Burger","readyInMinutes":45,"servings":6,"sourceUrl":"https://www.simplyrecipes.com/recipes/buffalo_burger/","openLicense":0,"image":"Buffalo-Burger-246916.jpg"},{"id":245166,"title":"Hawaiian Pork Burger","readyInMinutes":40,"servings":4,"sourceUrl":"https://www.simplyrecipes.com/recipes/hawaiian_pork_burger/","openLicense":0,"image":"Hawaiian-Pork-Burger-245166.jpg"},{"id":246009,"title":"Blue Cheese Burgers","readyInMinutes":55,"servings":4,"sourceUrl":"https://www.simplyrecipes.com/recipes/blue_cheese_burgers/","openLicense":0,"image":"Blue-Cheese-Burgers-246009.jpg"},{"id":219957,"title":"Carrot & sesame burgers","readyInMinutes":50,"servings":6,"sourceUrl":"https://www.bbcgoodfood.com/recipes/11011/carrot-and-sesame-burgers","openLicense":0,"image":"Carrot---sesame-burgers-219957.jpg"},{"id":607109,"title":"Turkey Zucchini Burger with Garlic Mayo","readyInMinutes":45,"servings":6,"sourceUrl":"https://www.simplyrecipes.com/recipes/turkey_zucchini_burger_with_garlic_mayo/","openLicense":0,"image":"Turkey-Zucchini-Burger-with-Garlic-Mayo-607109.jpg"},{"id":864633,"title":"Banh Mi Burgers with Spicy Sriracha Mayo","readyInMinutes":35,"servings":4,"sourceUrl":"http://littlespicejar.com/banh-mi-burgers-with-spicy-sriracha-mayo/","openLicense":0,"image":"banh-mi-burgers-with-spicy-sriracha-mayo-864633.jpg"},{"id":219871,"title":"Halloumi aubergine burgers with harissa relish","readyInMinutes":20,"servings":4,"sourceUrl":"https://www.bbcgoodfood.com/recipes/2196638/halloumi-aubergine-burgers-with-harissa-relish","openLicense":0,"image":"Halloumi-aubergine-burgers-with-harissa-relish-219871.jpg"},{"id":246177,"title":"Grilled Beef and Mushroom Burger","readyInMinutes":30,"servings":3,"sourceUrl":"https://www.simplyrecipes.com/recipes/grilled_beef_and_mushroom_burger/","openLicense":0,"image":"Grilled-Beef-and-Mushroom-Burger-246177.jpg"},{"id":245343,"title":"Herbed Turkey Burger","readyInMinutes":30,"servings":8,"sourceUrl":"https://www.simplyrecipes.com/recipes/herbed_turkey_burger/","openLicense":0,"image":"Herbed-Turkey-Burger-245343.jpg"}],"baseUri":"https://spoonacular.com/recipeImages/","offset":0,"number":10,"totalResults":180,"processingTimeMs":241,"expires":1620276324802,"isStale":false}
'''
下面是当前有效的代码,并在上面给了我结果^:
'''
$curl = curl_init();
$recipe= $_POST['recipe'];
curl_setopt_array($curl, [
CURLOPT_URL => "https://spoonacular-recipe-food-nutrition-v1.p.rapidapi.com/recipes/search?query=.$recipe",
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: spoonacular-recipe-food-nutrition-v1.p.rapidapi.com",
"x-rapidapi-key: KEY_HIDDEN_FOR_SECURITY_PURPOSES"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
'''
解决方案
如果您只想在浏览器中显示格式化输出,您可以将响应包装在“pre”标签中。
echo "<pre>".$response."</pre>";
推荐阅读
- python - WeakValueDictionary setdefault 不创建不会使用的默认值
- azure - 如何仅将一个静态公共 IP 分配给 AKS-multiAZ 负载均衡器
- android - AndroidBiometricPrompt onAuthenticationError 返回错误错误
- javascript - 为什么不将 cookie 实现为映射(键值对)?
- git - Git - 如何更改子模块的 url/路径
- python - 在日期对象中转换字符串(包含其他字符串) - Python
- kotlin - 了解 Kotlin 泛型中初始化和赋值之间的区别
- php - 无法使用 Volley 库将数据传递给 POST 请求 - Android
- opencv - 单独用于行人检测的任何特殊算法?
- safari - 画布抛出错误'错误:未捕获(在承诺中):SecurityError:操作不安全。toDataURL@[本机代码] '