想要从刀片文件 Laravel 中的 Api(json 形式)获取数据

Want to Get data from Api (json form) in blade file Laravel

提问人:Timothy 提问时间:1/19/2022 更新时间:3/30/2022 访问量:1467

问:

这是我想要从中获取数据的 API

"meals": [
        {
            "idMeal": "52770",
            "strMeal": "Spaghetti Bolognese",
            "strDrinkAlternate": null,
            "strCategory": "Beef",
            "strArea": "Italian",
            "strInstructions": "Put the onion and oil in a large pan and fry over a fairly high heat for 3-4 mins. Add the garlic and mince and fry until they both brown. Add the mushrooms and herbs, and cook for another couple of mins.\r\n\r\nStir in the tomatoes, beef stock, tomato ketchup or purée, Worcestershire sauce and seasoning. Bring to the boil, then reduce the heat, cover and simmer, stirring occasionally, for 30 mins.\r\n\r\nMeanwhile, cook the spaghetti in a large pan of boiling, salted water, according to packet instructions. Drain well, run hot water through it, put it back in the pan and add a dash of olive oil, if you like, then stir in the meat sauce. Serve in hot bowls and hand round Parmesan cheese, for sprinkling on top.",
            "strMealThumb": "https://www.themealdb.com/images/media/meals/sutysw1468247559.jpg",
            "strTags": "Pasta,Meat",
            "strYoutube": "https://www.youtube.com/watch?v=-gF8d-fitkU",
            "strIngredient1": "onions",
            "strIngredient2": "olive oil",
            "strIngredient3": "garlic",
            "strIngredient4": "lean minced beef",
            "strIngredient5": "mushrooms",
            "strIngredient6": "dried oregano",
            "strIngredient7": "tomatoes",
            "strIngredient8": "hot beef stock",
            "strIngredient9": "tomato puree",
            "strIngredient10": "worcestershire sauce",
            "strIngredient11": "spaghetti",
            "strIngredient12": "parmesan",
            "strIngredient13": null,
            "strIngredient14": null,
            "strIngredient15": null,
            "strIngredient16": null,
            "strIngredient17": null,
            "strIngredient18": null,
            "strIngredient19": null,
            "strIngredient20": null,
            "strMeasure1": "2",
            "strMeasure2": "1tbsp",
            "strMeasure3": "1 clove",
            "strMeasure4": "500g",
            "strMeasure5": "90g",
            "strMeasure6": "1tsp",
            "strMeasure7": "400g can",
            "strMeasure8": "300ml",
            "strMeasure9": "1tbsp",
            "strMeasure10": "1tbsp",
            "strMeasure11": "350g",
            "strMeasure12": "Topping",
            "strMeasure13": "",
            "strMeasure14": "",
            "strMeasure15": "",
            "strMeasure16": null,
            "strMeasure17": null,
            "strMeasure18": null,
            "strMeasure19": null,
            "strMeasure20": null,
            "strSource": null,
            "strImageSource": null,
            "strCreativeCommonsConfirmed": null,
            "dateModified": null
        }
    ]
}

这是这部分的控制器,我使用 Http 从 api 获取数据。

<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;

class ApiController extends Controller
{
  public function index(){
        $response = Http::get('www.themealdb.com/api/json/v1/1/search.php?s=Arrabiata')->json();
        return view('foodData.meal', ['response'=>$response]);
  }
}

路线

Route::get('api', 'App\Http\Controllers\ApiController@index');

Blade 文件在刀片文件中,如何显示来自 api 的数据。

@foreach($response as $product)
  {{($product['strMeal'])}}
@endforeach

错误

索引未被发现

请帮我解决这个错误。我正在努力将数据导入刀片模板。我尝试对以下内容进行硬编码,但收到未定义的索引错误。.

php json laravel api 未定义索引

评论

2赞 N69S 1/19/2022
没有什么能帮助您获得更多的见解。我的猜测是你试图在它里面时访问.您应该转储变量并浏览它。dd($product)strMeal$product->meals[0]->strMeal
0赞 user3532758 1/19/2022
N695 说了什么。试试这个好玩:或者没有循环$product[0]['strMeal']$response['meals'][0]['strMeal']

答:

0赞 Nika Simon 3/30/2022 #1

尝试使用 获取数据。 然后解码并打印$data = file_get_contents('www.themealdb.com/api/json/v1/1/search.php?s=Arrabiata')$obj = json_decode($data)$obj->strMeal