WSDL WebServices for Salesforce using PHP
I’ve found developing for Salesforce with PHP to be somewhat of an undocumented nightmare. However, there are a few useful blog posts around that can make life easier. One is here, which takes you through creating a webservice on Salesforce and then accessing it via PHP calls (yes – an end-to-end example! hooray!) – thanks to ArrowPointe for that post.
One gotchya that I discovered while attempting to access Webservices was ensuring that you actually expose your Apex class as a webservice:
global class myClass {
webservice static Double getTotalSales() {
Double globalTotal = 0;
// Code goes here....
return globalTotal;
}
}
Additionally, on the PHP end, ensure you put the name of the class you are attempting to access in the NAMESPACE when you put your session ID in the salesforce header (see below). The below example assumes you have already connected to salesforce using the PHP Toolkit and the connection is in the $sfc variable:
$sessionID = $sfc->getSessionId();
$wsdl="soapclient/myClass.wsdl.xml";
$NAMESPACE = "http://soap.sforce.com/schemas/class/myClass";
$sforce_header = new SoapHeader($NAMESPACE, "SessionHeader",
array("sessionId" => $sessionID));
$client = new soapclient($wsdl);
$client->__setSoapHeaders(array($sforce_header));
try{
$result = $client->getTotalSales();
}
catch(Exception $e) {
print "<pre>";
print_r($e);
exit;
}
If you forget that step, you can end up with a [soapenv:Client] Element {}item invalid at this location error which is bloody hard to debug. Trust me.

Leave a Reply