Creating Accounts and Contacts with the Salesforce API using PHP/CURL
The Salesforce API provides a comprehensive method of accessing your Salesforce data, including Accounts and Contacts. This article will walk you through how to create Accounts and Contacts in Salesforce using the Salesforce API, PHP, and cURL.
Before we proceed, it’s important to have a Salesforce Developer Edition account, which you can get for free. You should also have some basic understanding of PHP and cURL.
Step 1: Salesforce Connected App Setup
Before you can interact with Salesforce data via API, you need to create a Connected App in Salesforce. Follow these steps to set up a Connected App:
- Log into Salesforce, then click on
App Manager
underApps
in theSetup
menu. - Click
New Connected App
. - Enter the necessary information such as
Name
,API Name
, andContact Email
. - In the
API (Enable OAuth Settings)
section, checkEnable OAuth Settings
. - Input a
Callback URL
(for development, you could use:http://localhost:8080
). - In the
Selected OAuth Scopes
, addFull access (full)
. - Click
Save
.
Salesforce will generate a Consumer Key
and Consumer Secret
for you – make a note of these.
Step 2: Obtain Access Token
Before we make any requests, we first need to obtain an access token. This is done by making a POST request to the Salesforce token request endpoint.
As authentication is a little more involved we created an entire article on this:
How To: Salesforce Authentication with Firebase/PHP-JWT
Step 3: Creating a New Account
Now that we have our access token and instance URL, we can proceed to create an account.
//Replace with your auth token received in the authorization request $authToken = 'REPLACEWITHYOURAUTHTOKEN'; //Set variables with data that is provided via POST/GET, from a webhook etc.... $accountName = 'Test Account'; $address = '123 Test St'; $city = 'Liberty'; $state = 'MO' $zipCode = '64068' $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://YOURSALESFORCEINSTANCE.my.salesforce.com/services/data/v53.0/sobjects/Account/', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS =>'{"Name": "'.$accountName.'","BillingStreet": "'.$address.'","BillingCity": "'.$city.'","BillingState": "'.$state.'","BillingPostalCode": "'.$zipCode.'"}', CURLOPT_HTTPHEADER => array( 'Content-Type: application/json', 'Authorization: Bearer '.$authToken, ), )); $response = curl_exec($curl); curl_close($curl); //Decode response $response = json_decode($response); //Store newly created accountID $accountID = $response->id;
This code creates a new account named ‘Test Account’. It sends a POST request to the ‘Account’ object in Salesforce with the account name. On success, Salesforce returns the ID of the newly created account, which is stored in the $account_id
variable.
Step 4: Creating a New Contact
Next, we will use a similar procedure to create a new contact and link it to the account we just created.
$firstName = 'John'; $lastName = 'Doe'; $email = 'john.doe@prodjex.com'; $title = 'Developer'; $phoneNumber = '5555555555'; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://YOURSALESFORCEINSTANCE.my.salesforce.com/services/data/v53.0/sobjects/Contact/', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS =>'{"FirstName": "'.$firstName.'","LastName": "'.$lastName.'","Email": "'.$email.'","Phone": "'.$phoneNumber.'","Title": "'.$title.'", "AccountId": "'.$accountID.'"}', CURLOPT_HTTPHEADER => array( 'Content-Type: application/json', 'Authorization: Bearer '.$authToken, ), )); $response = curl_exec($curl); curl_close($curl);
In this code, we’re creating a new contact named ‘John Doe’ and linking it to the previously created account. This is done by setting the AccountId
field of the contact to the ID of the account.
This code concludes our tutorial on how to create accounts and contacts in Salesforce using the Salesforce API, PHP, and cURL. Keep in mind that error handling and data validation have been left out for simplicity’s sake. You should always implement these in a production environment to ensure your application’s robustness and security.
Need a custom Salesforce integration? Contact us today for an estimate!