PHP 5’s SOAP extension and SalesForce

August 30th, 2005 | by admin | suraski.net

In the last few days I’ve been working on a piece of code in PHP that had to integrate with SalesForce. During the course of this work I bumped into some minor annoyances (or rather, a minor annoyance) with ext/soap, which I was able to quickly fix thanks to Dmitry’s excellently structured code. For those who are interested, you can now set custom SOAP headers using SoapClient::__setSoapHeaders(). This will cause SoapClient to send the headers on every subsequent access to a web service, conducted through the relevant SoapClient object. While you could specify custom SOAP headers before - it required overloading __call() and doing this work in userland, and now it’s no longer necessary.
On a slightly related note, I wanted to take this opportunity to ask how many of you have actually used ext/soap, and what’s your feedback about it. Is it working well? Is there anything missing? As in the past, success stories are also interesting, they’re equally interesting to “it’s broken!” responses.
As a special bonus, for those of you who integrate with SalesForce, and are still using the old SOAP APIs (or God forbid, are using a language other than PHP to do it), here’s a code snippet that demos how to connect to SalesForce. Note that because it uses the new SoapClient::__setSoapHeaders(), you’d need a fairly recent snapshot/CVS in order for that to work.

PHP:
<?php
// Connect
$sforce = new SoapClient(”sforce.wsdl”);  // obtain your own customized sforce.wsdl from sforce.com
$loginResult = $sforce->login(array(”username” => “joe@doe.com”, “password” => “xyz”));
$loginResult= $loginResult->result;
// Set connection data
$sforce->__setLocation($loginResult->serverUrl);
$sforce_header = new SoapHeader(’urn:enterprise.soap.sforce.com’, ‘SessionHeader’, array(’sessionId’ => $loginResult->sessionId));
$sforce->__setSoapHeaders(array($sforce_header));
// Query SalesForce for Leads whose first name is Bill
try {
    $result = $sforce->query(array(”queryString” => “select Id, FirstName, LastName from Lead where Lead.FirstName=’Bill’”));
    print_r($result);
} catch (Exception $e) {
    print_r($e);
}
// Change a lead
try {
    $lead->LastName = “Gates”;
    $lead->Id = “…”;  // needs to be a valid id
    $arg->sObjects = new SoapVar($lead, SOAP_ENC_OBJECT, “Lead”, “http://soapinterop.org/xsd”);
    print_r($sforce->update($arg));
    print “SUCCESS!”;
} catch (Exception $e) {
    print_r($e);
}
?>


During the course of writing this blog I came across a blog post from George Schlossnagle which may also be interesting. Even though I didn’t bump into the same issue, it may be that I just didn’t get far enough - so you may still need the workaround in there.

Tags: , , ,

Post a Comment