【问题标题】:Formatting API Json response using PHP to nicely display it on my website使用 PHP 格式化 API Json 响应以在我的网站上很好地显示它
【发布时间】:2021-07-26 11:31:12
【问题描述】:

所以我是使用 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;
}

'''

【问题讨论】:

  • 预期的行为是什么?什么叫“漂亮的展示”?服务器返回给您的是 JSON 编码的字符串。您可以使用$recipes = json_decode($response, true);,然后显示您想要的任何内容,例如:foreach ($recipes as $recipe) { echo $recipe['title']; }
  • @MichałHaracewiat 所以我添加了这些行,现在我得到的错误是: Undefined index: title in... ----------------- - 非法字符串偏移 'title' in...---------------- 试图访问 int 类型值的数组偏移量 ------------
  • 您是否尝试找出可能出现的问题?使用这个foreach($recipes['results'] as $recipe) { echo $recipe['title']; }

标签: php json api curl php-curl


【解决方案1】:

如果您只想在浏览器中显示格式化的输出,您可以将响应包装在“pre”标签中。

echo "<pre>".$response."</pre>";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-01
    • 2012-01-19
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 2022-12-10
    • 2021-10-16
    • 1970-01-01
    相关资源
    最近更新 更多