Salesforce SOAP API and PHP

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", 'YOUR_USERNAME');
	define ("salesforce_password", 'YOUR_PASSWORD');
	define ("salesforce_token", 'YOUR_TOKEN');
	define ("salesforce_wsdl", 'soapclient/partner2.wsdl.xml');
	
	require_once ('soapclient/SforcePartnerClient.php');
?>

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

<?php
	require ('inc_config.php');
	ini_set('soap.wsdl_cache_enabled', 0);
	ini_set('soap.wsdl_cache_ttl', 0);
	
	//Create a new Salesforce Partner object
	$connection = new SforcePartnerClient();

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

	//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
		//Otherwise, handle this exception
	}
?>

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 stdClass;
     $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.

17 Comments

  1. fdfd

    an error occur in soapclient\SforceBaseClient.php
    errors are

    Notice: Trying to get property of non-object in C:\wamp\www\developerforce-Force.com-Toolkit-for-PHP-329d109\soapclient\SforceBaseClient.php on line 169

    Notice: Trying to get property of non-object in C:\wamp\www\developerforce-Force.com-Toolkit-for-PHP-329d109\soapclient\SforceBaseClient.php on line 203

    Notice: Trying to get property of non-object in C:\wamp\www\developerforce-Force.com-Toolkit-for-PHP-329d109\soapclient\SforceBaseClient.php on line 205

  2. SG

    This is great – thanks! Any chance you could do something similar for retrieving data from SalesForce once you have the lead ID and want to get the record data?

  3. ahmeddirie

    Hi SG, thanks for the comment. I’ll create a new post on this later tonight.

  4. Nitesh Patil

    Hey I tried ur sample code.. But its not generating lead into salesforce please tell me where I am going wrong. I have posted question on stackoverflow. Please have a look.
    http://stackoverflow.com/questions/14191389/salesforce-to-magento-integration

    Thanks.

  5. ahmeddirie

    Hi Nitesh,

    The only thing I am noticing is that the area titled “Getting and Sending Data to Salesforce” doesn’t include the connection file to get the process going. So, you should be connected at all. Add the following line at the top of the file.

    include (‘inc_connection.php’);

  6. Toqeer Ahmed

    Fatal error: Class ‘SoapClient’ not found in /home/iamschol/public_html/www.mytoqi.com/new/soapclient/SforcePartnerClient.php on line 44
    I am getting this error. Why ?

  7. ahmeddirie

    Hi Toqeer,

    I would first check to see if you have SOAP functionality turned on and verify what version of PHP you are running. Create a php file with php_info() and look at the output there.

  8. KumarShyama

    Hi ahmeddirie,

    I am unable to create new lead.Please have a look.

    Thanks

  9. ahmeddirie

    Hi Kumar,

    That doesn’t tell me much, although I hope you get this resolved. Please refer to this updated post which covers contacts (similar flow to leads) and a few other things > http://ahmeddirie.com/technology/web-development/salesforce-php-soap-api-creating-contacts-accounts-and-associating-records/

  10. Jean

    Thank for this tutorial. I have followed your tutorial but i still can’t connected to salesforce ! i got a message : SYSTEM WARNING: SoapClient::__doRequest():
    Please could you have an idea? Thanks

  11. stylojack

    The function for storing the lead, where does that go?

  12. Jul

    Hi Great tutorial!!
    thanks for posting stuff like this, quick question
    I am getting a soap error, how can i check in the developer account if the soap functionality is turn on?
    Fatal error: Call to a member function __setSoapHeaders()
    Thanks

  13. Jul

    Hi Great tutorial
    I am getting a similar error to Toqueer, just wondering you said check if SOAP functionality is turn on, How can I check this?
    Thanks

  14. ahmeddirie

    Hi Jul,

    Please refer to this updated post which covers contacts (similar flow to leads) and a few other things > http://ahmeddirie.com/technology/web-development/salesforce-php-soap-api-creating-contacts-accounts-and-associating-records/

  15. Matt

    I had to specify ‘soapclient/partner.wsdl.xml’ instead of ‘soapclient/partner2.wsdl.xml’ in the config. This fixed the “Fatal error: Call to a member function __setSoapHeaders()” error.

    To clarify, I also used the PHP Tookit as described in the post, here; https://github.com/developerforce/Force.com-Toolkit-for-PHP

    Works great. Thanks man.

  16. subhranshu

    How could we pass attachment to salesforce from php?Any idea

  17. prabath

    can we create Order via php

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>