{"id":1980,"date":"2011-12-03T01:13:59","date_gmt":"2011-12-03T06:13:59","guid":{"rendered":"http:\/\/ahmeddirie.com\/?p=1980"},"modified":"2013-05-31T08:00:35","modified_gmt":"2013-05-31T13:00:35","slug":"salesforce-soap-api-and-php","status":"publish","type":"post","link":"https:\/\/ahmeddirie.com\/blog\/web-development\/salesforce-soap-api-and-php\/","title":{"rendered":"Salesforce SOAP API and PHP"},"content":{"rendered":"<p>Lately, I&#8217;ve been doing a lot of work integrating a Drupal content management system with Salesforce through it&#8217;s SOAP API. Along the way, I&#8217;ve come across many stumbling blocks in getting both systems to interact and simply play nice.<br \/>\n<!--more--><br \/>\nSome of the features included extending Drupal&#8217;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.<\/p>\n<p>Enough of the small talk, let&#8217;s start with the basics and get a connection going to Salesforce using PHP, and run some cool functionality.<\/p>\n<h2>Some Resources<\/h2>\n<p>It goes without saying, you&#8217;ll need a Developer Account with Salesforce. If you don&#8217;t already have one,<a href=\"http:\/\/developer.force.com\" title=\"Developer Force\"> click here<\/a> to get one.<\/p>\n<p>You&#8217;re also going to need the Force.com Toolkit for PHP. To download, <a href=\"https:\/\/github.com\/developerforce\/Force.com-Toolkit-for-PHP\" title=\"Force.com Toolkit for PHP\">click here<\/a>.<\/p>\n<p>And the last thing you&#8217;ll want to keep handy is the Web Services API Developer&#8217;s Guide, available <a href=\"http:\/\/www.salesforce.com\/us\/developer\/docs\/api\/index_Left.htm\" title=\"Web Services API Developer's Guide\">here<\/a>.<\/p>\n<h2>Connecting to Salesforce<\/h2>\n<p>You&#8217;ve downloaded the toolkit, and there&#8217;s some great stuff in there. For this example, you&#8217;ll only really need the soapclient folder and the following files.<\/p>\n<p>1- SforceBaseClient.php<br \/>\n2- SforceHeaderOptions.php<br \/>\n3- SforcePartnerClient.php<br \/>\n4- partner.wsdl.xml<\/p>\n<p>At the bottom of the partner.wsdl.xml file on line 3128, you&#8217;ll want to modify the link to point to your Developer login site (login.salesforce to test.salesforce)<br \/>\n<soap:address location=\"https:\/\/login.salesforce.com\/services\/Soap\/u\/20.0\"\/><\/p>\n<p>Keep those four files in its folder (soapclient) and let&#8217;s create your configuration file, inc_config.php<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\tdefine (&quot;salesforce_username&quot;, 'YOUR_USERNAME');\r\n\tdefine (&quot;salesforce_password&quot;, 'YOUR_PASSWORD');\r\n\tdefine (&quot;salesforce_token&quot;, 'YOUR_TOKEN');\r\n\tdefine (&quot;salesforce_wsdl&quot;, 'soapclient\/partner2.wsdl.xml');\r\n\t\r\n\trequire_once ('soapclient\/SforcePartnerClient.php');\r\n?&gt;\r\n<\/pre>\n<p>Now let&#8217;s create your connection file, inc_connection.php<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\trequire ('inc_config.php');\r\n\tini_set('soap.wsdl_cache_enabled', 0);\r\n\tini_set('soap.wsdl_cache_ttl', 0);\r\n\t\r\n\t\/\/Create a new Salesforce Partner object\r\n\t$connection = new SforcePartnerClient();\r\n\r\n\t\/\/Create the SOAP connection to Salesforce\r\n\ttry {\r\n\t\t$connection-&gt;createConnection(salesforce_wsdl);\r\n\t} catch (Exception $e) {\r\n  \t\t\/\/Salesforce could be down, or you have an error in configuration\r\n\t\t\/\/Check your WSDL path\r\n\t\t\/\/Otherwise, handle this exception\r\n\t}\r\n\r\n\t\/\/Pass login details to Salesforce\r\n\ttry {\r\n\t\t$connection-&gt;login(salesforce_username, salesforce_password.salesforce_token);\r\n\t} catch (Exception $e) {\r\n\t\t\/\/Make sure your username and password is correct\r\n\t\t\/\/Otherwise, handle this exception\r\n\t}\r\n?&gt;\r\n<\/pre>\n<p>In a perfect world, you should be connected with no problems. If you are having issues, ensure your configurations are correct. <\/p>\n<h2>Getting and Sending Data to Salesforce<\/h2>\n<p>Let&#8217;s run a simple function on one of the core objects in Salesforce. We&#8217;ll describe the properties of a core object and store data in one.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n     \/\/Describing the Leads object and printing the array\r\n     $describe = $connection-&gt;describeSObjects(array('Lead'));\r\n     print_r($describe);\r\n\r\n     \/\/Create New Lead\r\n     $leadFirstName = &quot;Ahmed&quot;;\r\n     $leadLastName = &quot;Dirie&quot;;\r\n     $leadCompany = &quot;Pick Yours Up&quot;;\r\n     $leadEmail = &quot;adirie@pickyoursup.com&quot;;\r\n\r\n     \/\/Creating the Lead Object\r\n     $lead = new stdClass;\r\n     $lead-&gt;type = 'Lead';\r\n     $lead-&gt;fields = array(\r\n          'FirstName' =&gt; $leadFirstName,\r\n          'LastName' =&gt; $leadLastName,\r\n          'Company' =&gt; $leadCompany,\r\n          'Email' =&gt; $leadEmail\r\n     );\r\n\r\n     \/\/Submitting the Lead to Salesforce\r\n     $result = $connection-&gt;create(array($lead), 'Lead');\r\n?&gt;\r\n<\/pre>\n<p>You should see your new lead on Salesforce.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lately, I&#8217;ve been doing a lot of work integrating a Drupal content management system with Salesforce through it&#8217;s SOAP API. Along the way, I&#8217;ve come across many stumbling blocks in getting both systems to interact and simply play nice.<\/p>\n","protected":false},"author":2,"featured_media":2586,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[152],"tags":[323,22,55,331,332,329],"_links":{"self":[{"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/posts\/1980"}],"collection":[{"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/comments?post=1980"}],"version-history":[{"count":25,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/posts\/1980\/revisions"}],"predecessor-version":[{"id":3915,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/posts\/1980\/revisions\/3915"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/media\/2586"}],"wp:attachment":[{"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/media?parent=1980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/categories?post=1980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/tags?post=1980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}