Home PHP Integration Between Salesforce and PHP

Integration Between Salesforce and PHP

We can easily access the salesforce objects, fields & data using below is tha basic steps to Integration Between Salesforce and PHP.
STEP 1:
Salesforce providing support to PHP and delivered the PHP Toolkit. Using this toolkit we can connect with salesforce and perform all API operations supported like Insert, Update, Delete, Retrieve the data and much more.

STEP 2:
Salesforce PHP Toolkit Requires is,
PHP 5.x
SOAP Enabled
SSL Enabled
cURL Enabled
OpenSSL Enabled

STEP 3:
Download the latest PHP toolkit using below URL

PHP Toolkit: http://wiki.developerforce.com/page/Force.com_Toolkit_for_PHP
unzip the folder(soapclient Folder) and paste it in your PHP Htdocs Folder (Ex: PHP Project Folder Name is “PHP_SFDC”)

STEP 4:
The WSDL file can be downloaded from wihin your Salesforce Org. Once you login to Salesforce, go to Setup > App Setup > Develop > API and then you can generate a Partner WSDL or Enterprise WSDL file from there and and save it to your PHP Project Folder(www/Htdocs/PHP_SFDC/partner.wsdl)

STEP 5:
In PHP Project Folder (PHP_SFDC), Create a new File called getRecord.php file and save it to www/Htdocs/PHP_SFDC/getRecord.php path

add the following lines to getRecord.php file,

EXAMPLE:1
Retriving the Salesforce Contact Record using PHP

[php]

<?php
ini_set(“soap.wsdl_cache_enabled”, “0”);

// PHP Tool Kit class scripts to login to Salesforce Org
require_once (‘../includes/soapclient/SforcePartnerClient.php’);
require_once (‘../includes/soapclient/SforceHeaderOptions.php’);

// Salesforce Login information
$wsdl = ‘partner.wsdl.xml’;    // PARTNER WSDL FILE PATH
$userName = “salesforce username”;
$password = “salesforce password”; // PASSWORD SHOULD BE COMBINATION OF “PASSWORD + SECURITY TOKEN”, if it’s a Developer ORG.

// Process of logging on and getting a salesforce.com session
$client = new SforcePartnerClient();
$client->createConnection($wsdl);
$loginResult = $client->login($userName, $password);

//Then perform to get your Contact records from Your Salesforce ORG
$query = “SELECT Id, FirstName, LastName, Phone from Contact”;
$response = $loginResult->query($query);
foreach ($response->records as $record)
{
echo ‘<tr>
<td>’.$record->Id.'</td>
<td>’.$record->fields->FirstName.'</td>
<td>’.$record->fields->LastName.'</td>
<td>’.$record->fields->Phone.'</td>
</tr>’;
}
?>

[/php]

EXAMPLE:2
Update the Salesforce Contact Record using PHP

In PHP Project Folder (PHP_SFDC), Create a new File called updateRecord.php file and save it to www/Htdocs/PHP_SFDC/updateRecord.php path

[php]

<?php
$fieldsToUpdate = array (
‘FirstName’ => ‘TheBlogReaders.com 1’,
‘City’ => ‘BANGALORE’,
‘Country’ => ‘INDIA’
);
$sObject1 = new SObject();
$sObject1->fields = $fieldsToUpdate;
$sObject1->type = ‘Contact’;
$sObject1->Id = $UPDATEID1; // YOU NEED TO MENTION THE UDATE FIELD ID

$response = $loginResult->update(array ($sObject1));
print_r($response);

} catch (Exception $e) {
print_r($loginResult->getLastRequest());
echo $e->faultstring;
}
?>

[/php]

EXAMPLE:3
UPSERT the Salesforce Contact Record using PHP

[php]

<?php
$sObject->fields[‘FirstName’] = ‘Bill’;
$sObject->fields[‘LastName’] = ‘Clinton’;
$upsertResponse = $loginResult->upsert(“Email”, array ($sObject));
?>

[/php]

EXAMPLE: 4
SEARCH in the Salesforce data using PHP

[php]

<?php
$search = ‘FIND {415*} IN PHONE FIELDS ‘.
‘RETURNING CONTACT(ID, PHONE, FIRSTNAME, LASTNAME), ‘.
‘LEAD(ID, PHONE, FIRSTNAME, LASTNAME), ‘.
‘ACCOUNT(ID, PHONE, NAME)’;
$searchResult = $loginResult->search($search);
print_r($searchResult);
?>

[/php]

You may also like

Leave a Comment