Contains code for basic rest API implementation in php using username\password flow for API only account.
- Salesforce Sandbox -> Setup -> Create -> Apps
- Create new custom app with Oauth Enabled & Device Flow Enabled
- Copy the Consumer Key & Consumer Secret
- Login using API only user
composer require myoutdesk/salesforcerest
Default API version is 42.0, production is off by default. Your wrapper can be tested using a mock handler.
use MyOutDesk\SalesforceRest\SalesforceClient;
$salesforceRest = new SalesforceRest(new \GuzzleHttp\Client());
$connected = $salesforceRest->connectApp(CONSUMER_KEY, CONSUMER_SECRET)
->asUser(SALESFORCE_USER, SALESFORCE_PASSWORD)
->authenticate();
if($connected) {
// good to go
}
$salesforceRest->create('Lead', [
'FirstName' => 'John',
'LastName' => 'Smith',
'Company' => 'Company Name, LLC'
]);
// Requires api 42.0
$leadOne = [
'firstName' => 'John',
'lastName' => 'Smith',
'Company' => 'Company Name, LLC'
];
$leadTwo = [
'firstName' => 'Alex',
'lastName' => 'Smith',
'Company' => 'Company Name, LLC'
];
$salesforceRest->insertCollection('Lead', [$leadOne, $leadTwo]);
// Get all fields
$salesforceRest->get('Lead', ID);
// Get only specific fields
$salesforceRest->get('Lead', ID, ['Phone', 'customfield__c', 'email']);
// Requires api 42.0
$salesforceRest->getCollection('Lead', [ID, ANOTHER_ID], ['firstName', 'lastName']);
$salesforceRest->update('Account', ID, ['Phone' => '123-1234-123'])
// Requires api 42.0
$leadOne = [
'id' => ID,
'firstName' => 'LEAD ONE',
// other fields here
];
$leadTwo = [
'id' => ANOTHER_ID,
'firstName' => 'LEAD TWO',
// other fields here
];
$salesforceRest->updateCollection('Lead', [ $leadOne, $leadTwo ]);
$salesforceRest->delete('Lead', ID);
// Requires api 42.0
$salesforceRest->deleteCollection([ID, ANOTHER_ID]);
$salesforceRest->search('FIND {[email protected]} IN ALL FIELDS RETURNING Lead(Id, Name, Email)');