Salesforce SOAP API and PHP

December 3, 2011

Lately, I’ve been doing a lot of work integrating a Drupal content management system with Salesforce through it’s SOAP API. Along the way, I’ve come across many stumbling blocks in getting both systems to interact and simply play nice.

Some of the features included extending Drupal’s authentication and authorization to happen between both systems, and building a front-end in Drupal that allows users to interact with and manipulate data residing on Salesforce.

Enough of the small talk, let’s start with the basics and get a connection going to Salesforce using PHP, and run some cool functionality.

Some Resources

It goes without saying, you’ll need a Developer Account with Salesforce. If you don’t already have one, click here to get one.

You’re also going to need the Force.com Toolkit for PHP. To download, click here.

And the last thing you’ll want to keep handy is the Web Services API Developer’s Guide, available here.

Connecting to Salesforce

You’ve downloaded the toolkit, and there’s some great stuff in there. For this example, you’ll only really need the soapclient folder and the following files.

1- SforceBaseClient.php
2- SforceHeaderOptions.php
3- SforcePartnerClient.php
4- partner.wsdl.xml

At the bottom of the partner.wsdl.xml file on line 3128, you’ll want to modify the link to point to your Developer login site (login.salesforce to test.salesforce)

Keep those four files in its folder (soapclient) and let’s create your configuration file, inc_config.php

<?php
     define ("salesforce_username", 'username@domain.com');
     define ("salesforce_password", 'YOURPASSWORD');
     define ("salesforce_token", 'YOURTOKEN');
     define ("salesforce_wsdl", 'soapclient/partner.wsdl.xml');

     require_once ('soapclient/SforcePartnerClient.php');
?>

Now let’s create your connection file, inc_connection.php

<?php
     require ('inc_config.php');

     //Create a new partner object
     $connection = new SforcePartnerClient();

     //Create the SOAP connection
     try {
          $connection->createConnection(salesforce_wsdl);
     } catch (Exception $e) {
          //Salesforce could be down, or you have an error in configuration
          //Check your WSDL path
     }

     //Pass login details to Salesforce
     try {
          $connection->login(salesforce_username, salesforce_password . salesforce_token);
     } catch (Exception $e) {
          //Make sure your username and password is correct
     }
?>

In a perfect world, you should be connected with no problems. If you are having issues, ensure your configurations are correct.

Getting and Sending Data to Salesforce

Let’s run a simple function on one of the core objects in Salesforce. We’ll describe the properties of a core object and store data in one.

<?php
     //Describing the Leads object and printing the array
     $describe = $connection->describeSObjects(array('Lead'));
     print_r($describe);

     //Create New Lead
     $leadFirstName = "Ahmed";
     $leadLastName = "Dirie";
     $leadCompany = "Pick Yours Up";
     $leadEmail = "adirie@pickyoursup.com";

     //Creating the Lead Object
     $lead = new sObject();
     $lead->type = 'Lead';
     $lead->fields = array(
          'FirstName' => $leadFirstName,
          'LastName' => $leadLastName,
          'Company' => $leadCompany,
          'Email' => $leadEmail
     );

     //Submitting the Lead to Salesforce
     $result = $connection->create(array($lead), 'Lead');
?>

You should see your new lead on Salesforce.

    Tags: , , , , ,