<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jeff Douglas - Technology, Coding and Bears... OH MY! &#187; PHP</title>
	<atom:link href="http://blog.jeffdouglas.com/category/technology/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jeffdouglas.com</link>
	<description>Get your head out of your #@! and into the clouds!</description>
	<lastBuildDate>Thu, 02 Feb 2012 11:57:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Using PHP to call an Apex Web Service</title>
		<link>http://blog.jeffdouglas.com/2009/04/05/using-php-to-call-an-apex-web-service/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=using-php-to-call-an-apex-web-service</link>
		<comments>http://blog.jeffdouglas.com/2009/04/05/using-php-to-call-an-apex-web-service/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 02:54:44 +0000</pubDate>
		<dc:creator>Jeff Douglas</dc:creator>
				<category><![CDATA[Apex]]></category>
		<category><![CDATA[Code Sample]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Salesforce]]></category>

		<guid isPermaLink="false">http://blog.jeffdouglas.com/?p=639</guid>
		<description><![CDATA[I had the &#8216;pleasure&#8217; the other day of integrating Drupal with Salesforce.com using PHP. I didn&#8217;t want to write all of my SOQL queries and business logic in my PHP scripts so I whipped up a quick Apex class and exposed it as a web service. I found a great blog post by Scott Hemmeter [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.jeffdouglas.com%2F2009%2F04%2F05%2Fusing-php-to-call-an-apex-web-service%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.jeffdouglas.com%2F2009%2F04%2F05%2Fusing-php-to-call-an-apex-web-service%2F&amp;source=jeffdonthemic&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I had the &#8216;pleasure&#8217; the other day of integrating Drupal with Salesforce.com using PHP. I didn&#8217;t want to write all of my SOQL queries and business logic in my PHP scripts so I whipped up a quick Apex class and exposed it as a web service. I found a <a href="http://sfdc.arrowpointe.com/2008/12/05/calling-apex-web-services-from-php/" target="_blank">great blog post by Scott Hemmeter</a> that really helped me out calling the service with PHP so I thought I would share the code and other observations.</p>
<p>First you will need to download the <a href="http://wiki.developerforce.com/index.php/PHP_Toolkit" target="_blank">PHP Toolkit</a>. The toolkit contains all of the PHP code necessary to make web service calls against your org. Please note that the contained Partner and Enterprise WSDLs are for Production/Developer orgs, so if you are running against a Sandbox, you will need to download the appropriate WSDL from that Sandbox.</p>
<p>For this example, I took my <a href="http://blog.jeffdouglas.com/2009/02/24/returning-contacts-and-leads-with-custom-wrapper-class/" target="_blank">Person wrapper class</a> and exposed it as a web serivce. It exposes a method that allows you to search for Contacts and Leads by email address and returns a List of generic Person objects.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">&nbsp;
global class PersonService {
&nbsp;
    global class Person {
&nbsp;
        webservice String id;
        webservice String firstName;
        webservice String lastName;
        webservice String company;
        webservice String email;
        webservice String phone;
        webservice String sObjectType;
&nbsp;
    }
&nbsp;
    webService static List&lt;person&gt; searchByEmail(String email) {
&nbsp;
        // list of Person objects to return
        List&lt;person&gt; people = new List&lt;person&gt;();
&nbsp;
        // issue the sosl search
        List&lt;list&lt;sobject&gt;&gt; searchResults = [FIND :email IN EMAIL FIELDS RETURNING
            Contact (Id, Account.Name, Email, Phone, FirstName, LastName),
            Lead (Id, Company, FirstName, LastName, Email, Phone)];
&nbsp;
        // cast the results by sObjec type
        List&lt;contact&gt; contacts = ((List&lt;contact&gt;)searchResults[0]);
        List&lt;lead&gt; leads = ((List&lt;lead&gt;)searchResults[1]);
&nbsp;
        // a each contact found as a Person
        for (Integer i=0;i&lt;contacts.size();i++) {
            Person p = new Person();
            p.id = contacts[i].Id;
            p.firstName = contacts[i].FirstName;
            p.lastName = contacts[i].LastName;
            p.company = contacts[i].Account.Name;
            p.email = contacts[i].Email;
            p.phone = contacts[i].Phone;
            p.sObjectType = 'Contact';
            people.add(p);
        }
&nbsp;
        // a each lead found as a Person
        for (Integer i=0;i&lt;leads.size();i++) {
            Person p = new Person();
            p.id = leads[i].Id;
            p.firstName = leads[i].FirstName;
            p.lastName = leads[i].LastName;
            p.company = leads[i].Company;
            p.email = leads[i].Email;
            p.phone = leads[i].Phone;
            p.sObjectType = 'Lead';
            people.add(p);
        }
&nbsp;
        System.debug('Returning people: '+people);
&nbsp;
        return people;
&nbsp;
    }
}</pre></td></tr></table></div>

<p>Log into Salesforce.com and donwload the WSDL file for the PersonService class (Setup -&gt; Develop -&gt; Apex Classes). Also, make sure the profile calling the Web servcie has access to this class.</p>
<p>The PHP page looks like the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">&nbsp;
&lt;?PHP
&nbsp;
require_once ('sfdc/SforcePartnerClient.php');
require_once ('sfdc/SforceHeaderOptions.php');
&nbsp;
// Salesforce.com credentials
$sfdcUsername = &quot;YOUR-USERNAME&quot;;
$sfdcPassword = &quot;YOUR-PASSWORD&quot;;
$sfdcToken = &quot;YOUR-SECURITYTOKEN&quot;;
// the email address to search for. could also use a post/get variable
$searchEmail = 'phpblogtest@noemail.com';
&nbsp;
$sfdc = new SforcePartnerClient();
// create a connection using the partner wsdl
$SoapClient = $sfdc-&gt;createConnection(&quot;sfdc/partner.wsdl.xml&quot;);
$loginResult = false;
&nbsp;
try {
    // log in with username, password and security token if required
    $loginResult = $sfdc-&gt;login($sfdcUsername, $sfdcPassword.$sfdcToken);
} catch (Exception $e) {
    global $errors;
    $errors = $e-&gt;faultstring;
    echo &quot;Fatal Login Error &lt;b&gt;&quot; . $errors . &quot;&lt;/b&gt;&quot;;
    die;
}
&nbsp;
// setup the SOAP client modify the headers
$parsedURL = parse_url($sfdc-&gt;getLocation());
define (&quot;_SFDC_SERVER_&quot;, substr($parsedURL['host'],0,strpos($parsedURL['host'], '.')));
define (&quot;_WS_NAME_&quot;, &quot;PersonService&quot;);
define (&quot;_WS_WSDL_&quot;, &quot;sfdc/&quot; . _WS_NAME_ . &quot;.wsdl.xml&quot;);
define (&quot;_WS_ENDPOINT_&quot;, 'https://' . _SFDC_SERVER_ . '.salesforce.com/services/wsdl/class/' . _WS_NAME_);
define (&quot;_WS_NAMESPACE_&quot;, 'http://soap.sforce.com/schemas/class/' . _WS_NAME_);
&nbsp;
$client = new SoapClient(_WS_WSDL_);
$sforce_header = new SoapHeader(_WS_NAMESPACE_, &quot;SessionHeader&quot;, array(&quot;sessionId&quot; =&gt; $sfdc-&gt;getSessionId()));
$client-&gt;__setSoapHeaders(array($sforce_header));
&nbsp;
echo _SFDC_SERVER_.&quot;br&quot;;
echo _WS_NAME_.&quot;br&quot;;
echo _WS_WSDL_.&quot;br&quot;;
echo _WS_ENDPOINT_.&quot;br&quot;;
echo _WS_NAMESPACE_.&quot;p&quot;;
&nbsp;
try {
&nbsp;
    // call the web service via post
    $wsParams=array('email'=&gt;$searchEmail);
    $response = $client-&gt;searchByEmail($wsParams);
    // dump the response to the browser
    print_r($response);
&nbsp;
// this is really bad.
} catch (Exception $e) {
    global $errors;
    $errors = $e-&gt;faultstring;
    echo &quot;Ooop! Error: &lt;b&gt;&quot; . $errors . &quot;&lt;/b&gt;&quot;;
    die;
}
&nbsp;
?&gt;</pre></td></tr></table></div>

<p>The PHP page should look something like the following:</p>
<p><img class="alignnone size-full wp-image-648" title="php-webservice" src="http://blog.jeffdouglas.com/wp-content/uploads/2009/04/php-webservice.png" alt="php-webservice" width="544" height="182" /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jeffdouglas.com/2009/04/05/using-php-to-call-an-apex-web-service/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

