This is just a quick setup I use when I need my Laravel 4 application to query an external API. It uses cURL to make the requests, so make sure you have it installed.
Create your directory structure like so, and add in the files in this Gist!
app/Acme/Api/Request.php
app/Acme/Facades/Api.php
app/Acme/ServiceProviders/ApiServiceProvider.php
Obviously you'll need to update Request.php
with the API details and any extra header information that needs sending over.
Make sure you have the Acme
(or whatever name you've chosen) directory autoloaded with composer, and you've ran composer dump-autoload
!
Next open up your app/config/app.php
file. Add the service provider to the providers
array. E.g:
'providers' => array(
...
'Acme\ServiceProviders\ApiServiceProvider',
),
You'll also need to load your Facade with the alias of 'Api' into the aliases
array. E.g:
'aliases' => array(
...
'Api' => 'Acme\Facades\Api',
),
You should now be good to go.
You'll now be able to access your Request class anywhere in your application using the Api
facade.
=> get( $path, $params = [] )
Api::get('users/all'); // http://my-api.com/users/all
Api::get('users/all', ['limit' => 10, 'order' => 'desc']); // http://my-api.com/users/all/?limit=10&order=desc
=> post( $path, $params = [] )
Api::post('users/add', ['first_name' => 'Elliot', 'last_name' => 'Hesp']);