zbowl/food-data-central-api

Food Data Central API 集成

dev-master 2023-01-23 21:16 UTC

This package is auto-updated.

Last update: 2024-09-27 19:23:52 UTC


README

这是一个 Food Data Central API 的 Laravel 包装器。它仍在开发中。

配置

发布配置。

php artisan vendor:publish --provider="zbowl\FoodDataCentral\FoodDataCentralServiceProvider" --tag="config"

将环境变量添加到您的 .env 文件中。

FOOD_DATA_CENTRAL_BASE_URI=
FOOD_DATA_CENTRAL_API_VERSION=
FOOD_DATA_CENTRAL_API_KEY=
FOOD_DATA_CENTRAL_API_TIMEOUT=
FOOD_DATA_CENTRAL_API_RETRY_TIMES=
FOOD_DATA_CENTRAL_API_RETRY_DELAY=

它们将在 food-data-central 配置中使用。

/*
     * The base url used to for Food Data Central.
     *
     */
    'baseUri' => env('FOOD_DATA_CENTRAL_API_URI', 'https://api.nal.usda.gov/fdc'),

    /*
     * The API version provided by Food Data Central.
     *
     */
    'apiVersion' => env('FOOD_DATA_CENTRAL_API_VERSION', 'v1'),
    /*
     * The API Key provided by Food Data Central.
     * https://api.data.gov/signup/
     * https://fdc.nal.usda.gov/api-key-signup.html
     *
     */
    'apiKey' => env('FOOD_DATA_CENTRAL_API_KEY', null),
    /*
     * How long before the connection timeout.
     * Default is 10
     */
    'timeout' => env('FOOD_DATA_CENTRAL_API_TIMEOUT', 10),
    /*
     * The number of times a retry is allowed.
     * This is not used by default.
     */
    'retryTimes' => env('FOOD_DATA_CENTRAL_API_RETRY_TIMES', null),
    /*
     * The time between retry attempts in milliseconds.
     * This is not used by default.
     */
    'retryMilliseconds' => env('FOOD_DATA_CENTRAL_API_RETRY_DELAY', null),

    /*
     * The path to the migrations.
     */
    'includeMigrations' => env('FOOD_DATA_CENTRAL_API_INCLUDE_MIGRATIONS', false),

用法

use zbowl\FoodDataCentralApi\Facade\FoodDataCentral;

public function search(Request $request)
{
    $request->validate([
        'query' => 'required|string',
    ]);
    $searchTerm = $request->searchInput;
    
    $foodDataCentral = new FoodDataCentral();
    $results = $foodDataCentral::getFoodsSearch($searchTerm);
    
    return view('search', compact('results'));
}

{
    $foodDataCentral = FoodDataCentral::class;
    $results = $foodDataCentral::getFoodsSearch('apple');
    return $results;
}

资源