PHP code example of frankkessler / salesforce-laravel-oauth2-rest

1. Go to this page and download the library: Download frankkessler/salesforce-laravel-oauth2-rest 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/ */

    

frankkessler / salesforce-laravel-oauth2-rest example snippets


Frankkessler\Salesforce\Providers\SalesforceLaravelServiceProvider::class,

'Salesforce'    => Frankkessler\Salesforce\Facades\Salesforce::class,

'salesforce.logger' => $class_or_class_name

SALESFORCE_API_DOMAIN=na1.salesforce.com
SALESFORCE_OAUTH_CALLBACK_URL=https://yourdomain.com/salesforce/callback
SALESFORCE_OAUTH_DOMAIN=login.salesforce.com
SALESFORCE_OAUTH_CONSUMER_TOKEN=YOUR_CLIENT_ID_FROM_CREATED_APP
SALESFORCE_OAUTH_CONSUMER_SECRET=YOUR_CLIENT_SECRET_FROM_CREATED_APP

Route::get('salesforce/login', '\Frankkessler\Salesforce\Controllers\SalesforceController@login_form');
Route::get('salesforce/callback', '\Frankkessler\Salesforce\Controllers\SalesforceController@process_authorization_callback');

$objectType = 'Account';
$objectId = 'OBJECT_ID';

$result = Salesforce::sobject()->get($objectId, $objectType);

if($result->success){
    $name = $result->sobject['Name'];
}

$objectType = 'Account';
$objectData = [
    'Name'          => 'Acme',
    'Description'   => 'Account Description',
];

$result = Salesforce::sobject()->insert($objectType, $objectData);

if($result->success){
    $id = $result->id;
}

$objectType = 'Account';
$objectId = 'OBJECT_ID';
$objectData = [
    'Name'          => 'Acme',
    'Description'   => 'Account Description',
];

$result = Salesforce::sobject()->update($objectId, $objectType, $objectData);

if($result->success){
    //no data returned
}

$objectType = 'Account';
$objectId = 'OBJECT_ID';

$result = Salesforce::sobject()->delete($objectId, $objectType);

if($result->success){
    //no data returned
}

$soql = 'SELECT Id, Name FROM Account LIMIT 1';

$result = Salesforce::query()->query($soql);

if($result->success && $result->totalSize > 0){
    foreach($result->records as $record){
        $account_name = $record['Name'];
    }
}

//since all records are grabbed at once and all records will be in memory, test the max size of the data you would like to use with this function before running in production.  Large data sets could throw PHP OUT OF MEMORY errors.

$soql = 'SELECT Id, Name FROM Account LIMIT 1';

$result = Salesforce::query()->queryFollowNext($soql);

if($result->success && $result->totalSize > 0){
    foreach($result->records as $record){
        $account_name = $record['Name'];
    }
}

$sosl = 'FIND {Acme} IN ALL FIELDS RETURNING Account(Id, Name ORDER BY LastModifiedDate DESC LIMIT 3)'; 

$result = Salesforce::query()->search($sosl);

if($result->success && $result->totalSize > 0){
    foreach($result->records as $record){
        $account_name = $record['Name'];
    }
}

$operationType = 'insert';
$objectType = 'Account';
$objectData = [
    [
        'Name'          => 'Acme',
        'Description'   => 'Account Description',
    ],
    [
        'Name'          => 'Acme2',
        'Description'   => 'Account Description2',
    ],
];

$result = Salesforce::bulk()->runBatch($operationType, $objectType, $objectData);

if($result->id){
    $id = $result->id;
}

$operationType = 'upsert';
$objectType = 'Account';
$objectData = [
    [
        'ExternalId__c' => 'ID1',
        'Name'          => 'Acme',
        'Description'   => 'Account Description',
    ],
    [
        'ExternalId__c' => 'ID2',
        'Name'          => 'Acme2',
        'Description'   => 'Account Description2',
    ],
];

$result = Salesforce::bulk()->runBatch($operationType, $objectType, $objectData, ['externalIdFieldName' => 'ExternalId__c']);

if($result->id){
    foreach ($result->batches as $batch) {
        echo $batch->numberRecordsProcessed;
        echo $batch->numberRecordsFailed;
        foreach ($batch->records as $record) {
            if(!$record['success']){
                echo 'Record Failed: '.json_encode($record);
            }
        }
    }
}


$operationType = 'query';
$objectType = 'Account';
$objectData = 'SELECT Id, Name FROM Account LIMIT 10';

$result = Salesforce::bulk()->runBatch($operationType, $objectType, $objectData);

if ($result->id) {
    $id = $result->id;
    foreach ($result->batches as $batch) {
        foreach ($batch->records as $record) {
            $account_id = $record['Id'];
        }
    }
}

$operationType = 'query';
$objectType = 'Account';
$objectData = 'SELECT Id, Name FROM Account';

$result = Salesforce::bulk()->runBatch($operationType, $objectType, $objectData, [
    'contentType' => 'CSV',
    'Sforce-Enable-PKChunking' => [
        'chunkSize' => 2500,
    ],
]);

if ($result->id) {
    $id = $result->id;
    foreach ($result->batches as $batch) {
        foreach ($batch->records as $record) {
            $account_id = $record['Id'];
        }
    }
}

$operationType = 'query';
$objectType = 'Account';
$objectData = 'SELECT Id, Name FROM Account LIMIT 10';

$result = Salesforce::bulk()->runBatch($operationType, $objectType, $objectData,[
    'batchProcessor' => CustomBulkBatchProcessor::class, //Class must implement Frankkessler\Salesforce\Interfaces\BulkBatchProcessorInterface
]);

if ($result->id) {
    $id = $result->id;
}

$uri = 'custom_apex_uri_get';

$result = Salesforce::custom()->get($uri);

if($result->http_status == 200){
    $body = $result->raw_body;
}

$uri = 'custom_apex_uri_post';

$result = Salesforce::custom()->post($uri);

if($result->http_status == 200){
    //success
}
bash
php artisan vendor:publish
bash
php artisan migrate

$privateKeyPassphrase = null;
$csrString = '';
$privateKeyString = '';
$certString = '';

$config = [
    'private_key_type' => \OPENSSL_KEYTYPE_RSA,
    'digest_alg' => 'sha256',
    'private_key_bits' => 2048,
];

$dn = array(
    "countryName" => "US",
    "stateOrProvinceName" => "New York",
    "localityName" => "New York",
    "organizationName" => "SalesforceLaravel",
    "organizationalUnitName" => "SalesforceLaravel",
    "commonName" => "SalesforceLaravel",
    "emailAddress" => "[email protected]"
);

$privateKey = openssl_pkey_new($config);

$csr = openssl_csr_new($dn, $privateKey);

$sscert = openssl_csr_sign($csr, null, $privateKey, 365);

openssl_csr_export($csr, $csrString);
file_put_contents(__DIR__.'/../csr.csr', $csrString);

openssl_x509_export($sscert, $certString);
file_put_contents(__DIR__.'/../public.crt', $certString);

openssl_pkey_export($privateKey, $privateKeyString, $privateKeyPassphrase);
file_put_contents(__DIR__.'/../private.key', $privateKeyString);