Getting Record Data from Salesforce SOAP API using PHP

Recently, I was asked to write a post on how to obtain a lead record information from Salesforce when you have the Lead ID. This post will go over this and the code required to get this functioning. One thing to note is that Lead IDs in Salesforce are alphanumeric and 18 characters long (e.g. 00A5150409abcyQAHB).

It would be easier obtaining the record with an email address provided that this field is unique. My guess is that this would depend on your installation. Both ways, ID or email, the code to query for records are the exact same – with minor changes of course.

Although the connection code is copied below, please follow this post for more information on how to setup your SOAP API connection and the files that are required.

Configuration File – inc_config.php

It’s good practice to keep the configuration file separate from function and view files. It just makes for cleaner code and better file arrangement.

	define ("salesforce_username", 'YOUR_USERNAME');
	define ("salesforce_password", 'YOUR_PASSWORD');
	define ("salesforce_token", 'YOUR_TOKEN');
	define ("salesforce_wsdl", 'soapclient/partner.wsdl.xml');
	
	require_once ('soapclient/SforcePartnerClient.php');

Connection File – inc_connection.php

This file will have to reference your configuration file for the connection parameters.

	require ('inc_config.php');

	//Disabling the WSDL cache
	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 error in configuration
		//Check your WSDL path. 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
	}

Main File – index.php
Obtaining Record Data with Lead ID or Email Address

The query to Salesforce’s database is much like SQL with a few extra features that make it really shine. For this call, it’s like any other MySQL call to a database.

	//We need this file to start the connection
	include ('inc_connection.php');
	
	//Obtaining object containing record data with Lead ID
	$queryLead = "SELECT Id, LastName, FirstName, Salutation, Title, Company, Street, City, State, PostalCode, Country, Phone, Email, Website FROM Lead WHERE Id='00A5150409abcyQAHB'";
	$resultLead = $connection->query($queryLead);
	
	//Obtaining object containing record data with Email address
	$queryEmail = "SELECT Id, LastName, FirstName, Salutation, Title, Company, Street, City, State, PostalCode, Country, Phone, Email, Website FROM Lead WHERE Email='somebody@email.com'";
	$resultEmail = $connection->query($queryEmail);
	
	//Checking if the query for a Lead with ID returns an object
	if ($resultLead->size) {
		//The lead exists
		//Display or parse values from the object
	}
	else {
		echo "This lead does not exist in Salesforce.";
	}

You should be able to look up data in your Salesforce database just fine. If you’re having issues connecting, make sure you verify your connection parameters as well as your SOAP endpoint in the WSDL file.

2 Comments

  1. Eric Hollebone

    Love the article but a potential easier approach would be the lighter weight REST api

    http://www.salesforce.com/us/developer/docs/api_rest/index.htm

  2. ahmeddirie

    Thanks Eric. That is totally true. I got a chance to play with Pardot’s API using REST and I gotta admit, I prefer REST over SOAP. I’ll dedicate a later post to Salesforce using that.

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>