Salesforce / REST API Client for Laravel 5

Installs: 2 015

Dependents: 0

Stars: 16

Watchers: 4

Forks: 15

Open Issues: 2

Language: PHP

v2.0.2 2015-07-24 18:19 UTC


Laravel Latest Stable Version Total Downloads License Build Status

Salesforce/ REST API client for Laravel. It provides access to restricted Salesforce information via Oauth 2.0. REST is a lightweight alternative to the SOAP API and is useful for mobile users.

While this package is built for Laravel, it has been decoupled so that it can be extended into any framework or vanilla PHP application.


If you are upgrading to Version 2.0, be sure to re-publish your config file.

Forrest can be installed through composer. Open your composer.json file and add the following to the require key:

"omniphx/forrest": "2.*"

Next run composer update from the command line to install the package.

If you are using Laravel, add the service provider and alias to your config/app.php file:


'Forrest' => Omniphx\Forrest\Providers\Laravel\Facades\Forrest::class

For Laravel 4, add Omniphx\Forrest\Providers\Laravel4\ForrestServiceProvider in app/config/app.php. Alias will remain the same.

Lumen Installation

You should copy the config file from src/config/config.php and add it to a forrest.php configuration file under a config directory in the root of your application.

Then you'll utilize the Lumen service provider by registering it in the bootstrap/app.php file.



You will need a configuration file to add your credentials. Publish a config file using the artisan command:

php artisan vendor:publish

You can find the config file in: config/forrest.php

For Laravel 4, run php artisan config:publish omniphx/forrest. It will be found in app/config/omniphx/forrest/config.php

Getting Started

Setting up a Connected App

  1. Log into to your Salesforce org
  2. Click on Setup in the upper right-hand menu
  3. Under Build click Create > Apps
  4. Scroll to the bottom and click New under Connected Apps.
  5. Enter the following details for the remote application:
    • Connected App Name
    • API Name
    • Contact Email
    • Enable OAuth Settings under the API dropdown
    • Callback URL
    • Select access scope (If you need a refresh token, specify it here)
  6. Click Save

After saving, you will now be given a Consumer Key and Consumer Secret. Update your config file with values for consumerKey, consumerSecret, loginURL and callbackURI.


Forrest will come with the following routes included in it's package.

Feel free to overwrite these in your routes.php file. Your routes can be called anything you like, but the callback must what you used in your Connected App settings.

Web Server authentication flow
Route::get('/authenticate', function()
    return Forrest::authenticate();

Route::get('/callback', function()

    $url = Config::get('forrest::authRedirect');

    return Redirect::to($url);
Username-Password authentication flow

With the Username Password flow, you can directly authenticate with the Forrest::authenticate() method.

To use this authentication you must add your username, and password to the config file. Security token might need to be ammended to your password unless your IP address is whitelisted.

Route::get('/authenticate', function()

    $url = Config::get('forrest::authRedirect');

    return Redirect::to($url);
Custom login urls

Sometimes users will need to connect to a sandbox or custom url. To do this, simply pass the url as an argument for the authenticatation method:

Route::get('/authenticate', function()
    $loginURL = '';

    return Forrest::authenticate($loginURL);

Note: You can specify a default login URL in your config file.

Basic usage

After authentication, your app will store an encrypted authentication token which can be used to make API requests.

Query a record

Forrest::query('SELECT Id FROM Account');


    "totalSize": 2,
    "done": true,
    "records": [
            "attributes": {
                "type": "Account",
                "url": "\/services\/data\/v30.0\/sobjects\/Account\/001i000000xxx"
            "Id": "001i000000xxx"
            "attributes": {
                "type": "Account",
                "url": "\/services\/data\/v30.0\/sobjects\/Account\/001i000000xxx"
            "Id": "001i000000xxx"

If you are querying more than 2000 records, you response will include:

"nextRecordsUrl" : "/services/data/v20.0/query/01gD0000002HU6KIAW-2000"

Simply, call Forrest::next($nextRecordsUrl) to return the next 2000 records.

Create a new record

Records can be created using the following format.

$body = ['Name' => 'New Account'];
    'method' => 'post',
    'body'   => $body]);

Update a record

Update a record with the PUT method.

$body = [
    'Name'  => 'Acme'
    'Phone' => '555-555-5555'];

    'method' => 'put',
    'body'   => $body]);

Upsert a record

Update a record with the PATCH method and if the external Id doesn't exist, it will insert a new record.

$body = [
    'Phone' => '555-555-5555',
    'External_Id__c' => 'XYZ1234'];

    'method' => 'patch',
    'body'   => $body]);

Delete a record

Delete a record with the DELETE method.

Forrest::sobjects('Account/001i000000xxx', ['method' => 'delete']);

XML format

Change the request/response format to XML with the format key or make it default in your config file.


API Requests

With the exception of the search and query resources, all resources are requested dynamically using method overloading.

First, determine which resources you have access to by calling:



    [sobjects] => /services/data/v30.0/sobjects
    [connect] => /services/data/v30.0/connect
    [query] => /services/data/v30.0/query
    [theme] => /services/data/v30.0/theme
    [queryAll] => /services/data/v30.0/queryAll
    [tooling] => /services/data/v30.0/tooling
    [chatter] => /services/data/v30.0/chatter
    [analytics] => /services/data/v30.0/analytics
    [recent] => /services/data/v30.0/recent
    [process] => /services/data/v30.0/process
    [identity] =>
    [flexiPage] => /services/data/v30.0/flexiPage
    [search] => /services/data/v30.0/search
    [quickActions] => /services/data/v30.0/quickActions
    [appMenu] => /services/data/v30.0/appMenu

Next, call resources by referring to the specified key. For instance:




Resource urls can be extended by passing additional parameters into the first argument:


You can also add optional parameters to requests:


Additional API Requests


If a refresh token is set, the server can refresh the access token on the user's behalf. Refresh tokens are only for the Web Server flow.


If you need a refresh token, be sure to specify this under access scope in your Connected App. You can also specify this in your configuration file by adding 'scope' => 'full refresh_token'. Setting scope access in the config file is optional, the default scope access is determined by your Salesforce org.


This will revoke the authorization token. The session will continue to store a token, but it will become invalid.


Returns all currently supported versions. Includes the verison, label and link to each version's root:


Returns list of available resources based on the logged in user's permission and API version.


Returns information about the logged-in user.


For a complete listing of API resources, refer to the REST API Developer's Guide

Custom Apex endpoints

If you create a custom API using Apex, you can use the custom() method for consuming them.


Additional options and parameters can be passed in like this:

Forrest::custom('/myEndpoint', [
    'method' => 'post',
    'body' => ['foo' => 'bar'],
    'parameters' => ['flim' => 'flam']]);

Read Creating REST APIs using Apex REST for more information.

Raw Requests

If needed, you can make raw requests to an endpoint of your choice.

Forrest::post('/services/data/v20.0/endpoint', ['my'=>'param']);
Forrest::put('/services/data/v20.0/endpoint', ['my'=>'param']);
Forrest::patch('/services/data/v20.0/endpoint', ['my'=>'param']);

Raw response output

By default, this package will return the body of a response as either a deserialized JSON object or a SimpleXMLElement object.

There might be times, when you would rather handle this differently. To do this, simply use any format other than 'json' or 'xml' and the code will return a Guzzle response object.

$response = Forrest::sobjects($resource, ['format'=> 'none']);
$content = (string) $response->getBody(); // Guzzle response

Event Listener

This package makes use of Guzzle's event listers

Event::listen('forrest.response', function($request, $response) {
    dd((string) $response);

For more information about Guzzle responses and event listeners, refer to their documentation.