Salesforce is a cloud-based Customer relationship management software it brings customer and company together it is one integrated platform that includes sales, marketing and service provide 360% view of the customer. In the article, we will see how to connect Salesforce using PHP for performing integration tests and elaborate contains details on how to authenticate test GET and POST operation within Salesforce.
We shall be using PHP for integration and to execute PHP code we need XAMPP / WAMP server. In this specific case we will be using the latter.
Background to XAMPP Server
XAMPP is the most popular and Open source platform for PHP development environment it a cross-platform for APACHE, MYSQL, MARIA DB, PHP and Perl it is one of the simplest and lightweight local serves
Installation Steps for XAMPP
In case you do not have XAMPP already set up, here are the steps by which you can set it up.
Step 1: Download XAMPP server SETUP file form APACHE website it is available for different platform like Windows, LINUX and MAC-OS.
Step 2: Double click on step file and accept the user agreement and click install
Step 3: Click on XAMPP control panel and START Apache and MYSQL server as shown below screenshot
On successfully installation of XAMPP Server now we need to connect the server to Salesforce for this we using Salesforce “Connected App” it is pre-built functionality provided by Salesforce to connect third party application below are configuration steps for the same.
Step to Configure Salesforce “Connect App”
Next step is to set up the Salesforce Connect App. Here are the steps for the same.
Step 1: Log-in to Salesforce with administrator credential
Step 2: Step ⇒ Quick Search = App ⇒ Click on App Manager
Step 3: Click on Create “Connect App” as shown in below screenshot
Step 4: Fill the required details and click on Save and Continue. You will be re-directed to below screen.
After creating the Connected App
- Click on Click to reveal a link to get Consumer secret.
- Make a note of “Consumer Key “ and “Consumer secret”, as you need these details to authenticate the external application.
Goto Manage->OAuth Policies-> select IP restrictions as “Relax IP restrictions”.
Step by step process for Authentication, Get and Post-operation
Salesforce Authentication:
- Salesforce authentication is required to get access token and instance URL for connecting PHP with Salesforce.
- PHP Code for authentication are as follows:
- Session_start( ): It creates or resumes the current session identifier through GET or POST request.
- For the authentication, we need to give the following details:
- Client_id
- Client_secret
- Username
- Password
- Grant_type
- These above details can be obtained from the connected app.
- cURL: It stands for client URL
- In-Line 16 curl_init() is used for initializing the session.
- curl_setopt is used to set options for the cURL.
- Inline 19 CURLOPT_RETURNTRANSFER is used to set the return transfer to enable for curl
- In line 20 CURLOPT_POST is used to set the method of posting variables for curl.
- Inline 22 CURLOPT_POSTFIELDS is used to set the variable.
- Inline 24 curl_exec is used for executing the curl session and it is called after curl initialization.
- Inline 34 curl_close is used to close the session.
- Inline 38 and 41, get a response from salesforce as an access token and instance URL.
- Inline 47, Header will redirect to the required location.
- Save the above code in a local server such as filename.php.Use the following path
(Local Disk(c)->Xampp->htdocs) - Run the code in localhost as shown in the diagram:
- From above we will get access token and instance URL
Fetching records using Get Operation
- Get operation is used to fetch the objects from salesforce.
- Session_start( ): It creates or resumes the current session identifier through GET or POST request.
- Inline 7, a function named show_account is used to fetch a record.
- In the function required parameters are provides such as $access_token, $instance_url, $id .
- cURL: It stands for client URL
- In-Line 9 curl_init() is used for initializing the session.
- curl_setopt is used to set options for the cURL.
- Inline 11 CURLOPT_HEADER is used to get the header data separately.
- Inline 12 CURLOPT_RETURNTRANSFER is used to set the return transfer to enable for curl
- In line 15 curl_exec is used for executing the curl session and it is called after curl initialization.
- Inline 22 curl_close is used to close the session
- Inline 23, get a response from salesforce as account details.
- Inline 35, return the function value.
- Above PHP code is written to fetch the account from salesforce and save in local server as filename.php.
- In this, we use the same access token and instance URL of authentication to get the data.
- For fetching data, show_account function is used wherein return we are passing the id to get details of an account.
Creating records using Post-operation
- In this step, we are creating an account using post action.
- Below code represents about the creation of the record and save as Filename.php:
- Session_start( ): It creates or resumes the current session identifier through GET or POST request.
- Inline 5, a function named create_account is used to create a record.
- In the function required parameters are provides such as $access_token, $instance_url, $id and so on as per requrement .
- cURL: It stands for client URL
- In-Line 8 curl_init() is used for initializing the session.
- curl_setopt is used to set options for the cURL.
- Inline 10 CURLOPT_HEADER is used to get the header data separately.
- Inline 11 CURLOPT_RETURNTRANSFER is used to set the return transfer to enable for curl.
- Inline 14 CURLOPT_POST is used to set the method of posting variables for curl.
- Inline 15 CURLOPT_POSTFIELDS is used to set the variable.
- Inline 16 curl_exec is used for executing the curl session and it is called after curl initialization.
- Inline 22 curl_close is used to close the session.
- Inline 23, get a response from salesforce as the account is created or not.
- Inline 28, return the function value.
How we can help
Our team can help you customize and integrated Salesforce as per your business process. Read about our Salesforce Integration Services. Reach out to us for a free assessment of your business needs.