PHP API Wrapper for Open Food Facts, the open database about food.
With Composer:
composer require openfoodfacts/openfoodfacts-php
This is the most basic way of creating the API:
$api = new OpenFoodFacts\Api('food','fr');
$product = $api->getProduct('3057640385148');
In the example above you access the "food" database, limited to the French language/country scope. The first parameter is either
to decide which product database you want to use.
The second parameter decides the language/country scope of the chosen database: f.e. "world" or "de" or "fr".
For more details on this topic: see the API Documentation
These are all the parameters you really need for basic usage.
As return types for $api->getProduct
you get an Document::class
Object.
This may also be an Object of Type FoodProduct::class
,PetProduct::class
, BeautyProduct::class
depending on which API you are creating.
These objects inherit from the more generic Document::class
In the example above, we use the 'food' API and there will get a FoodProduct::class
For getting a first overview the Document::class
has a function to return an array representation(sorted) for a first start.
$product = $api->getProduct('3057640385148');
$productDataAsArray = $product->getData();
The other parameters are optional and for a more sophisticated use of the api (from a software development point of view):
An example in code is found here: cached_example.php
LoggerInterface: A logger which decieds where to log errors to (file, console , etc)
ClientInterface: The HTTP Client - to adjust the connection configs to your needs and more
see: Guzzle HTTP Client
CacheInterface: To temporarily save the results of API request to improve the performance and to reduce the load on the API- Server
see: PSR-16 Simple Cache
git checkout -b my-new-feature
)vendor/bin/phpunit
&& cs-fixer vendor/bin/php-cs-fixer fix
git commit -am 'Add some feature'
)git push origin my-new-feature
)If you use this SDK, feel free to open a PR to add your application in this list.