A package to consume api smoothly
Explore the docs »
View Package
·
Report Bug
·
Request Feature
This package is useful to consume API's, here is the instruction for installation and usage.
-
To install this package using Packagist
-
On the root of your project run following command
composer require lde/api-helper
-
This command will install package with dependency
- Note : This package comes with autodiscover capabilities if you are using Laravel >=5.5 then you don't need to register providers and aliases, but you have to publish provider step-4.
-
After installing package some configuration need to be done to use package, Now go to your Project->config->app.php open this file.
-
We need to register our provider and aliases to use it in project so add the following lines for providers
Lde\ApiHelper\ApiHelperServiceProvider::class,
-
Now we have to add aliases in same file add following line in aliases
'ApiHelper' => Lde\ApiHelper\ApiHelperFacade::class,
-
Next step is to publish config files to use with packages so, run the following command in your terminal to publish config file to config folder.
php artisan vendor:publish --provider="Lde\ApiHelper\ApiHelperServiceProvider"
- This will publish config file naming api_helper.php into config folder.
-
To use this package you need to add following namespace where you want to use this package.
use ApiHelper;
-
This method is use to add headers.
-
It accept name and value as parameter, Here you can set only one header at a time.
$headers['Accept'] = "application/json"; $headers['Content-Type'] = "application/json"; ApiHelper::addHeaders($headers);
-
We will get response in form of object of ApiBuilder.
-
This method is use to set api that we are going to use from api_helper.php , there is httpbin and mokbin is define so you have to pass the name that you want to use.
-
You can also define your own api end point at api_helper.php in config file.
ApiHelper::api('httpbin')->method_to_call();
-
The snippet indicates how you can connect particular api and access their method.
-
This will return object of ApiResponse.
- Here you will get object in response, In each response you will get success either true or false
- You will also get status code for more information about response please check below doc.
- https://docs.guzzlephp.org/en/latest/psr7.html#responses