Salesforce SOAP and Bulk API Integration in PHP

Author - Ashish Gautam

Implementing Salesforce SOAP and Bulk API Integration in PHP

Salesforce SOAP API:
Salesforce SOAP API is a coding method to use our Org’s information with the help of inbuilt functions and SOQL query. With the help of these function, we can access or modify the data of our Org. We can use CRUD operations using SOAP API for create, Retrieve, Update, Delete and Upsert operations. Any programming language that supports Web Services, can implement Salesforce SOAP API. To use Salesforce SOAP API in php we need to use SFDC PHP Toolkit, we can download this toolkit from the github link below:

https://github.com/developerforce/Force.com-Toolkit-for-PHP

Salesforce Bulk API:
Bulk API process the queries in a more efficient way for a large number of records that could be from thousands to some millions. We can use SOQL queries for create, Retrieve, Update, Delete and Upsert operations in batches and these batches run asynchronously on the specified Org. if we use only SOAP API to work on a large number of records than it takes more time to process which is not practical to use. That’s why using the Bulk API with SOAP API to process a large number of record can reduce the time to process the records. To use Salesforce Bulk API in php we need to use Client for Salesforce Bulk Api, we can download this toolkit from the github link below:

https://github.com/shubinmi/salesforce-bulk-api

Integration in PHP:
To create a connection of the Org for all the operation using SOAP api, we need a username, password, and security token of the user from which account we need to connect to the Salesforce Org.
Here is the code to connect to the Salesforce Org:

require_once ('../SFDCPHPtoolkit/soapclient/SforceEnterpriseClient.php');
require_once ('../SFDCPHPtoolkit/soapclient/SforceHeaderOptions.php');
require_once ('../salesforce-bulk-api-client-master/src/BulkApiClient.php');

try {
   $sfUsername = 'XXXX';
   $sfPassword = 'XXXX';
   $sfSecurityToken = 'XXXX';
   $sfdcEnvironment = 'sandbox';
   $mySforceConnection = new SforceEnterpriseClient ();
   if ($sfdcEnvironment == "sandbox")
       $mySforceConnection->createConnection ( "../SFDCPHPtoolkit/soapclient/enterprise_sandbox.wsdl.xml" );
   else
       $mySforceConnection->createConnection ( "../SFDCPHPtoolkit/soapclient/enterprise.wsdl.xml" );
$result = $mySforceConnection->login ( $sfUsername, $sfPassword . $sfSecurityToken );
  
   if ($result == '0') {
      echo "error connecting to the Org";
	exit;
   }
} catch ( Exception $ex ) {
   console . log ( $ex );
}
// we need end point and sessionId of the connection to create bulk API connection
$endPoint = $mySforceConnection->getLocation ();
$sessionId = $mySforceConnection->getSessionId ();
$myBulkApiConnection = new BulkApiClient ( $endPoint, $sessionId );
$myBulkApiConnection->setLoggingEnabled ( true ); // optional, but using here for demo purposes
$myBulkApiConnection->setCompressionEnabled ( true ); // optional, but recommended. defaults to true.

// Creating data
$file = 'test_final_2.csv';
$handle     = fopen($file, "r") or die("file not opened to read");
$testcsv = '';
$headrecord=array('LastName','Email');
foreach ( $headrecord as $header )
   $head .= '"' . $header . '",';
$head = rtrim ( $head, ',' );
$head .= "\n";
while(($data = fgetcsv($handle)) !== FALSE)
{
   foreach($data as $data1)
       $testcsv .= '"'.$data1.'",';
   $testcsv = rtrim($testcsv[$i],",");
   $testcsv .= "\n";
}
fclose($handle);
// Creating Job
$job = new JobInfo ();
$job->setObject ( "Contact" );
$job->setOpertion ( "insert" );
$job->setContentType ( "CSV" );
$job->setConcurrencyMode ( "Parallel" ); //can also set to Serial
$job->setExternalIdFieldName("Email");
$job1 = $myBulkApiConnection->createJob ( $job );
$batch = $myBulkApiConnection->createBatch($job1, $testcsv);
// CLOSE THE JOB
$myBulkApiConnection->updateJobState($job->getId(), "Closed");

// Reteriving Batch result
$batchResults = $myBulkApiConnection->getBatchResults($job1[$i]->getId(), $batch[$i]->getId());

// Printing the batch result
print_r($batchResults);

// clear log buffer
$myBulkApiConnection->clearLogs();
Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *