Forked from:
bjsmasth/php-salesforce-rest-api
Cleeng/php-salesforce-rest-api
jerkob/php-salesforce-rest-api-forked
- SOQL with Doctrine DBAL: https://github.com/codelicia/trineforce
- Interacting with Salesforce objects: https://github.com/roblesterjr04/EloquentSalesForce
composer require ehaerer/php-salesforce-rest-api
Setting up a Connected App
- Log into your Salesforce org
- Click on Setup in the upper right-hand menu
- Under Build click
Create > Apps
- Scroll to the bottom and click
New
under Connected Apps. - 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)
- Click Save
After saving, you will now be given a consumer key and consumer secret. Update your config file with values for consumerKey
and consumerSecret
See a full example which could be tested with ddev local in /example folder folder.
$options = [
'grant_type' => 'password',
'client_id' => 'CONSUMERKEY', /* insert consumer key here */
'client_secret' => 'CONSUMERSECRET', /* insert consumer secret here */
'username' => 'SALESFORCE_USERNAME', /* insert Salesforce username here */
'password' => 'SALESFORCE_PASSWORD' . 'SECURITY_TOKEN' /* insert Salesforce user password and security token here */
];
$salesforce = new \EHAERER\Salesforce\Authentication\PasswordAuthentication($options);
/* if you want to login to a Sandbox change the url to https://test.salesforce.com/ */
$endPoint = 'https://login.salesforce.com/';
$salesforce->setEndpoint($endPoint);
$salesforce->authenticate();
/* if you need access token or instance url */
$accessToken = $salesforce->getAccessToken();
$instanceUrl = $salesforce->getInstanceUrl();
/* create salesforceFunctions object with instance, accesstoken and API version */
$salesforceFunctions = new \EHAERER\Salesforce\SalesforceFunctions($instanceUrl, $accessToken, "v52.0");
$query = 'SELECT Id,Name FROM ACCOUNT LIMIT 100';
$additionalHeaders = ['key' => 'value'];
/* returns array with the queried data */
$data = $salesforceFunctions->query($query, $additionalHeaders);
$data = [
'Name' => 'Some name',
];
$additionalHeaders = ['key' => 'value'];
/* returns the id of the created object or full response */
$accountId = $salesforceFunctions->create('Account', $data, $additionalHeaders);
$fullResponse = $salesforceFunctions->create('Account', $data, $additionalHeaders, true);
$newData = [
'Name' => 'another name',
];
$additionalHeaders = ['key' => 'value'];
/* returns statuscode */
$salesforceFunctions->update('Account', $id, $newData, $additionalHeaders);
$newData = [
'Name' => 'another name',
];
$additionalHeaders = ['key' => 'value'];
/* returns statuscode */
$salesforceFunctions->upsert('Account', 'API Name/ Field Name', 'value', $newData, $additionalHeaders);
$additionalHeaders = ['key' => 'value'];
$salesforceFunctions->delete('Account', $id, $additionalHeaders);
$additionalHeaders = ['key' => 'value'];
$salesforceFunctions->describe('Account', $additionalHeaders);
$additionalHeaders = ['key' => 'value'];
$salesforceFunctions->customEndpoint('apex/myCustomEndpoint', $data, 200, $additionalHeaders);
- updated Guzzle dependency to ^7.4
- added full example in /example folder with ddev local configuration
- added option to add additional headers like on https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/headers.htm
- updated documentation
- [breaking] switched version parameter in constructor to the end
- added method to use custom endpoints
- added describe method
- switched to PHP >7.0
- renamed class from CRUD to SalesforceFunctions
- added dependency to ext-json in composer package