Skip to content

Instantly share code, notes, and snippets.

@Ehesp
Created April 26, 2014 19:24
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Ehesp/11328670 to your computer and use it in GitHub Desktop.
Save Ehesp/11328670 to your computer and use it in GitHub Desktop.
Laravel 4 Api Request Class - Service Provider + Facade

Laravel 4 Api Request Class

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.

Setup

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.

Usage

You'll now be able to access your Request class anywhere in your application using the Api facade.

GET Request

=> 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 Request

=> post( $path, $params = [] )

Api::post('users/add', ['first_name' => 'Elliot', 'last_name' => 'Hesp']);
<?php namespace Acme\Facades;
use Illuminate\Support\Facades\Facade;
class Api extends Facade {
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'api'; }
}
<?php namespace Acme\ServiceProviders;
use Illuminate\Support\ServiceProvider;
class ApiServiceProvider extends ServiceProvider {
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
// Register 'api' instance container to our Request object
$this->app['api'] = $this->app->share(function($app)
{
return new \Acme\Api\Request;
});
}
}
<?php namespace Acme\Api;
/*
|--------------------------------------------------------------------------
| Request Class
|--------------------------------------------------------------------------
|
| This class is used when querying your API.
|
| * Class is called via the Facade 'Api'
| * E.g. Api::get('api');
|
*/
class Request {
/**
* @var string The domain location of the API
*/
private static $domain = 'http://my-api.com';
/**
* @var array Any additional headers to be sent with each request
*/
private static $requestHeaders = [
'apiKey' => '123456789'
];
/**
* @var bool Set whether the cURL request will be over HTTP or HTTPS
* false = HTTP
* true = HTTPS
*/
private static $secure = false;
/**
* Class construction
*/
public function __construct() {
$this->curl = curl_init();
curl_setopt_array( $this->curl, [
CURLOPT_SSL_VERIFYPEER => self::$secure,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => self::$requestHeaders,
]
);
}
/**
* Class destructor
*/
public function __destruct() {
curl_close( $this->curl );
}
/**
* HTTP GET Request
*/
public function get( $path, $params = [] ){
curl_setopt_array(
$this->curl, [
CURLOPT_URL => self::$domain . '/' . $path . '/?' . http_build_query( $params ),
]
);
return curl_exec( $this->curl );
}
/**
* HTTP POST Request
*/
public function post( $path, $params = [] ) {
curl_setopt_array(
$this->curl, [
CURLOPT_URL => self::$domain . '/' . $path,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $params,
]
);
return curl_exec( $this->curl );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment