PHP code example of aleostudio / salesforcerest

1. Go to this page and download the library: Download aleostudio/salesforcerest library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aleostudio / salesforcerest example snippets

sh

eoStudio\SalesForceRest\SalesForceRest;

// OAuth credentials.
$config = [
    'clientId'     => 'YOUR_CLIENT_ID',
    'clientSecret' => 'YOUR_CLIENT_SECRET',
    'callbackUrl'  => 'https://your_domain/oauth_callback_url',
];

// Your stored data to avoid the authentication every time (empty object at the first time).
$storedToken = (object) [];
$storedToken = (object) [
    'accessToken'  => 'YOUR_STORED_ACCESS_TOKEN',
    'refreshToken' => 'YOUR_STORED_REFRESH_TOKEN',
    'instanceUrl'  => 'YOUR_STORED_INSTANCE_URL',
    'tokenExpiry'  => 'YOUR_STORED_TOKEN_EXPIRY_DATE'
];

// Main instance.
$sf = new SalesForceRest($config);


// If we already have a valid token object, we can bypass the auth flow.
if (!empty((array) $storedToken)) {
    // Sets the stored token into our SalesForce instance.
    $sf->setToken($storedToken);
} else {
    // OAuth authentication.
    // Set $authorize to true to force the auth or leave it to false if you want to use your stored token.
    $authorize = true;
    $sf->authentication($storedToken, $authorize);
}


// Query an entity using the SOQL syntax.
$response = $sf->query('SELECT Id, Name, Title, FirstName, LastName, Email from Contact LIMIT 10');
foreach ($response['records'] as $row) {
    echo 'ID: '.$row['Id'].' - Name: '.$row['Name'].' - Email: '.$row['Email'].'<br/>';
}


// Full entity fields list example.
$fields = $sf->getEntityFields('Contact');
foreach ($fields as $field) {
    echo 'Name: '.$field['name'].' - Label: '.$field['label'].' - Type: '.$field['type'].'<br />';
}


// CRUD methods list.
$results = $sf->query('SELECT Id, Name from Contact LIMIT 100');
$new_id  = $sf->create('Contact', ['FirstName'=>'John', 'LastName'=>'Doe', 'Email'=>'[email protected]']);
$update  = $sf->update('Contact', '0030b00002KgsnvAAB', ['FirstName'=>'Johnnnnn', 'LastName'=>'Doeeee', 'Title'=>null]);
$delete  = $sf->delete('Contact', '0030b00002KgsnvAAB');

// Helper methods list.
$lists   = $sf->getEntityLists('Contact');
$fields  = $sf->getEntityFields('Contact');
$fOnList = $sf->getEntityFields('Contact', 'LIST_ID');
$item    = $sf->getItem('Contact', '0030b00002KgsnvAAB');
$items   = $sf->getItems('Contact', 'LIST_ID');

// Auth methods list.
$authInstance = $sf->authentication((object)[], true);
$tokenObject  = $sf->getToken();
$accessToken  = $sf->getAccessToken();
$instanceUrl  = $sf->getInstanceUrl();
$sf->setToken($tokenObject);

sh
phpunit --bootstrap vendor/autoload.php tests/SalesForceRestTest.php 
sh
composer test tests/SalesForceRestTest.php