Tuesday, 28 April 2015

Force.com Toolkit for PHP ( Creating a contact into the salesforce using Force.com Toolkit)

Hi,

The Force.com PHP Toolkit provides an easy-to-use wrapper for the Force.com Web Services SOAP API, presenting SOAP client implementations for both the enterprise and partner WSDLs.

Download php tool kit."https://github.com/developerforce/Force.com-Toolkit-for-PHP" into your machine.


To run php samples we need a server.

Xampp:It is a free open source cross-platfrom web server package.

1)install 'Xampp' server .(while installing i gave name "PHPXAMPP")
Download Xampp Server:https://www.apachefriends.org/download.html
 After install 



2)2)Suppose i installed in F drive 
 Go to: F:\PHPXAMPP\htdocs
 Create your own folder, name it as you own (balajiPhPforce).



3)Now create your first php program in xampp and name it as “welcome.php”:

<!DOCTYPE html>
<html>
<body>

<?php
echo "Welcome to php world!";
?>  

</body>
</html>
4)Now double click on “XAAMP CONTROL PANEL” on desktop and START “Apache”

5)Type localhost on your browser and press enter:
It will show the following:


6)Now type the following on browser:
http://localhost/balajiPhPforce/
Below screenshot shows PHP files created under folder “balajiPHPforce”

Output:
1)Coming to Salesforce.com example .First we have to copy "soapClient" folder from PHP Toolkit to our folder "balajiPhPforce" which is in "htdocs" folder.


Create a "PHP" with name "ContactrecordCreation.php" page by using snippets as shown below.
Code:
====
Creating a contact record into salesforce.com.

Eg:

<?php
require_once ('soapclient/SforcePartnerClient.php');
require_once ('soapclient/SforceEnterpriseClient.php');
define("USERNAME", "username");
define("PASSWORD", "password");
$mySforceConnection = new SforcePartnerClient();
$mySforceConnection->createConnection("soapclient/partner.wsdl.xml");
 if (isset($_SESSION['partnerSessionId'])) {
                    $location = $_SESSION['partnerLocation'];
                    $sessionId = $_SESSION['partnerSessionId'];

                    $mySforceConnection->setEndpoint($location);
                    $mySforceConnection->setSessionHeader($sessionId);

                    echo "Used session ID for partner<br/><br/>\n";
                } else {
                    $mySforceConnection->login(USERNAME, PASSWORD);

                    $_SESSION['partnerLocation'] = $mySforceConnection->getLocation();
                    $_SESSION['partnerSessionId'] = $mySforceConnection->getSessionId();

                    echo "Logged in with partner<br/><br/>\n";
                }
 $records = array();
$records[0] = new SObject();
                $records[0]->fields = array(
'FirstName' => 'balaji',
                    'LastName' => 'malemarpuram',
                    'email' => 'mbalaji105@gmail.com',
                    'BirthDate' => '1988-05-25'
                );
$records[0]->type = 'Contact';
$response = $mySforceConnection->create($records);
$ids = array();
                foreach ($response as $i => $result) {
                    echo ($result->success == 1) 
                            ? $records[$i]->fields["FirstName"]." "
                                .$records[$i]->fields["LastName"]." "
                                .$records[$i]->fields["email"]
                                ." created with id ".$result->id."<br/>\n"
                            : "Error: ".$result->errors->message."<br/>\n";
                    array_push($ids, $result->id);
                }
?>

Run this file as explained above.

Output:
---------
I referred the following references:


https://developer.salesforce.com/page/Tksample.php
https://developer.salesforce.com/page/Getting_Started_with_the_Force.com_Toolkit_for_PHP

http://www.tutorialspoint.com/shorttutorials/run-a-php-program-in-xampp-server


All the best.