Salesforce Toolkit for PHP

Author posted by Jitendra on Posted on under category Categories Force.com, Salesforce, Web Technology and tagged as Tags , with 15 Comments on Salesforce Toolkit for PHP

Example of using PHP toolkit in Salesforce with Sample code

In a web development PHP has prooved itself as one of the major contributor in the world. There are lots of websites which are built over the PHP and the best example is facebook. PHP is very powerfull, easy to learn and the best thing is that it is open source.

Following the popularity of PHP, salesforce has also started providing support to PHP and delivered the Toolkit for it. Using this toolkit we can connect with salesforce and perform all API operations supported like Insert, update, delete, retrieve etc.

Download Salesforce PHP Toolkit Example – Zip

We can download the toolkit from this URL – Gits – https://github.com/developerforce/Force.com-Toolkit-for-PHP

In a toolkit downloaded, only important folder is “soapclient“. Copy that folder in your project directory. The Force.com PHP Toolkit requires PHP 5.x with the cURL, SOAP and OpenSSL PHP modules.

Next step is to download “Partner WSDL” from Salesforce (we have already seen the example of  Enterprise WSDL so this time i am using Partner WSDL). To download Partner WSDL, Click Your Name | Setup | Develop | API to display the WSDL download page. Download the Partner WSDL and save as “PartnerWSDL.xml”.

Add below code snippet in your PHP page to retrieve the information from Salesforce using toolkit.

define("USERNAME", "Your salesforce username");
define("PASSWORD", "salesforce password");
define("SECURITY_TOKEN", "salesforce security token");

require_once ('soapclient/SforcePartnerClient.php');

$mySforceConnection = new SforcePartnerClient();
$mySforceConnection->createConnection("PartnerWSDL.xml");
$mySforceConnection->login(USERNAME, PASSWORD.SECURITY_TOKEN);

$query = "SELECT Id, FirstName, LastName, Phone from Contact";
$response = $mySforceConnection->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>';
 }

As you can see in above code, we have imported the php file “SforcePartnerClient.php” which is provided by tookit. Add the UserName, Password and Security token in above code. To access the field from Object use syntax “recordset->fields->FieldName”.
The final output of the example attached in this article would look like:

force.com toolkit for PHP toolkit
force.com toolkit for PHP toolkit

Download Salesforce PHP Toolkit Example – Zip

You can refer this article also on developerforce.

Related posts

  • Shiva

    This is ok.But Can you retrieve any document from Salesforce using php?

  • CIS

    hi,

    i have copied the SFDC kit on root and tried but it’s not working.

  • Hi there. I am pretty green at developing php apps but I want to take a shot at this.. I have a slightly different requirement though and i was wondering if you could help me through it. I am trying to check to see if a specific record is a client based on a field in salesforce. This post gets me part the way there but I need to be able to tell based on email comparison whether or not :

    1. The email exists in sales force…
    2. And for that record see if the field customer is a “yes” or “blank”
    3. if yes respond yes if blank respond no

    is this possible?

  • Richa Upadhyay

    Hi, How to upsert picklist values into salesforce via PHP integration?? picklist is order status field?

    Thanks!!
    Richa

  • HI, I can’t seem to make the connection right. I am having a Fatal error: Uncaught SoapFault exception: [HTTP] Could not connect to host. Any advice how to solve this?

    Thanks.

    Benessa

    • DO you have debug enabled in your PHP server and any idea on which page and line ? Issue might be in configuration.

  • Praveen Kumar

    Hi,

    I am new to PHP. Can any one help me to install all this software’s in windows environment.(“PHP 5.x with the cURL, SOAP and OpenSSL”) that is required for the force.com tool kit

  • Praveen Kumar

    Hi Jitendra,

    The XAMPP worked fine for me.I have a small doubt.I am having the PHP form where the user fill all the details and click submit

    How should i pass the values (Filled in the form )dynamically to create a record in salesforce

    Suppose, I have First Name and Last name In My from and user submits .How to capture the values and create the record in salesforce.

    Thanks
    Praveen

    • Anshul.sf

      Hi Praveen,

      My scenario was something near to your post. As i am waiting for help from anyone.

      My task is like i have to integrate salesforce with external website. The developer at the external end will push data in PHP script format.
      Then how do i get those data into salesforce and save. Do i need to code to get that..
      Any suggestions will be helpful..!

      • Praveen Kumar

        Hi Anshul,

        If you have a php developer trying top push the data from the external website .you need to give him the credentials to authenticate and save the data on salesforce.

        Steps to follow:

        1) please create an connected App in your salesforce
        Setup->create->apps ->Connected App (Through which you will get the Client_ID and client _Secret )

        “grant_type” => “password”,
        “TOKEN_URL”=>”https://replaceinstace.salesforce.com/services/oauth2/token”
        “client_id” => “GET FROM Connected APP”,

        “client_secret” => “GET FROM Connected APP”,

        “username”==> “salesforce username(maybe an API user used for integration or our username “,

        “password” => “Salesforce password”,

        “security_token”=> “salesforce security token fro the user”,

        If you give this values to the php developer he will be able to use the credentials and try to push the data to salesforce.

        Thanks
        Praveen

        • Anshul.sf

          Thank alot Praveen.

          So, as you said, There is no need for me to code anything, Instead i have to pass the credentials to the PHP Developer.
          Once he pushes the data into salesforce will i be able to use it directly.

          Thanks,
          Anshul

  • Shubham Sharma

    SELECT Id, FirstName, LastName, Phone from Contact Where is contact table

  • Pingback: Bootstrapping the Force.com PHP Toolkit – Wrap up | The Salesforce Mobile guy!()