Search Content

WhitePapers


Sales Force Automation Comparison Guide

Businesses of all sizes can benefit by automating all aspects of their sales processes with an SFA (Sales Force Automation) solution. But due to the sheer number of features that most SFA solutions...Read More


How to Buy a Phone System

Considering a new phone system for your business? The Phone System Buyer's Guide from VoIP-News provides you with all of the information you need to make a more informed decision. The Guide helps you...Read More


Which CMS Is Right For Me?

If you're wondering which CMS is the right one for your organization, this comprehensive guide will take you through the various options available, detailing the pros and cons of each. Download...Read More


Oracle Magazine

Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest...Read More




View All Whitepapers

Calling Apex Web Services from PHP

Apex Code can be exposed as a Web Service and made available outside of your Salesforce environment (e.g. from a PHP page). This approach essentially lets you build a personal API into your Salesforce org and eliminates the need for calling standard API methods in PHP code where it is vulnerable to your configuration [...]

Apex Code can be exposed as a Web Service and made available outside of your Salesforce environment (e.g. from a PHP page). This approach essentially lets you build a personal API into your Salesforce org and eliminates the need for calling standard API methods in PHP code where it is vulnerable to your configuration changes.

Working with the folks over at MK Partners on some recent projects, I’ve learned how to call into Apex Web Services from PHP. It’s actually pretty easy. Special thanks for Simon Fell for helping me through a particularly tricky part.

Apex Web Service Class

A simple web service class is below. The method we call from PHP is myMethod. There are 2 inner classes that are used to capture inputs and send back outputs to PHP.

global class MyWebService {

// A class to accept an array of input records
(e.g. product/amount combinations)
global class myInputs{
webservice Id productId;
webservice Double amount;
}

// A class to send back as an output to PHP
global class myOutputs{
webservice String errorMessage;
webservice Boolean success;
webservice List<myInputs> inputs;
webservice Id contactId;
}

// The actual web service method we will call
webservice static myOutputs myMethod(Id contactId,
List<myInputs> inputs){

/*
* Write a bunch of code here to do all kinds of stuff.
*/

myOutputs output = new myOutputs();
output.errorMessage = 'No errors here.';
output.success = true;
output.inputs = inputs;
output.contactId = contactId;
return output;

}
}
PHP

Login like you normally would using the PHP toolkit. Nothing new here. The final part is defining some constants for use later when we call the web service.

// Include the PHP Toolkit
require_once('salesforceAPI/SforcePartnerClient.php');
require_once('salesforceAPI/SforceHeaderOptions.php');

// Login
$sfdc = new SforcePartnerClient();
$SoapClient = $sfdc->createConnection
('salesforceAPI/wsdl.xml');
$loginResult = false;
$loginResult = $sfdc->login('user@domain.com',
'password' . 'securitytoken');

// Define constants for the web service. We'll use these later
$parsedURL = parse_url($sfdc->getLocation());
define ("_SFDC_SERVER_", substr($parsedURL['host'],
0,strpos($parsedURL['host'], '.')));
define ("_WS_NAME_", 'MyWebService');
define ("_WS_WSDL_", _WS_NAME_ . '.xml');
define ("_WS_ENDPOINT_", 'https://' . _SFDC_SERVER_ .
'.salesforce.com/services/wsdl/class/' . _WS_NAME_);
define ("_WS_NAMESPACE_", 'http://soap.sforce.com/schemas/class/
' . _WS_NAME_);

Next we will call the web service. First thing to do is setup a Soap Client and modify the headers. Then we are setting up some fake data that maps to the expected inputs of the myMethod method in the web service. Then we actually call the web service.

// SOAP Client for Web Service
$client = new SoapClient(_WS_WSDL_);
$sforce_header = new SoapHeader(_WS_NAMESPACE_,
"SessionHeader", array("sessionId" => $sfdc->getSessionId()));
$client->__setSoapHeaders(array($sforce_header));

// Setup fake data to send into the web service
$prodAmtArray = array();
$prodAmtArray[] = array('productId'=>
'01t60000000lvBN','amount'=>100);
$prodAmtArray[] = array('productId'=>
'01t60000000lvBS','amount'=>200);

$wrkArray = array(
'contactId'=>'0036000000nVtpT',
'inputs'=>$prodAmtArray
);

// Call the web service
$response = $client->myMethod($wrkArray);

// Output results to browser
echo "<p><pre>" . print_r($response, true) .
"</pre></p>";
echo "Contact Id is " . $response->result->contactId;
Results

Below displays what those 2 echo statements output.

stdClass Object
(
[result] => stdClass Object
(
[contactId] => 0036000000nVtpTAAS
[errorMessage] => No errors here.
[inputs] => Array
(
[0] => stdClass Object
(
[amount] => 100
[productId] =>
01t60000000lvBNAAY
)
[1] => stdClass Object
(
[amount] => 200
[productId] =>
01t60000000lvBSAAY
)
)
[success] => 1
)
)
Contact Id is 0036000000nVtpTAAS
One last little trick

One annoyance during the development of Apex Web Services is having to continually generate a WSDL for the web service. When testing, I would find that I would continually need to put a new WSDL on the web server. I decided to automate this where I could add a variable to the queryString and have PHP refresh the WSDL on my web server. You need the cURL module for this, but most PHP installs have it.

$ch = curl_init();
$fp = fopen(_WS_WSDL_, "w");
curl_setopt($ch, CURLOPT_URL, _WS_ENDPOINT_);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_COOKIE, 'sid='.$
sfdc->getSessionId());
setcookie("sid", $sfdc->getSessionId(), 0,
 "/", ".salesforce.com", 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_exec($ch);
fclose($fp);
curl_close($ch);

If you have experience with this kind of integration, please comment here on other tips & tricks.


Related Forex Forecast Software Articles

Build your very own seat at the strategy table


Everybody seems to be clamoring for a seat at the strategy table. It came up at the IxDA-SF talk on the Worth of Design, it’s covered in an engaging post by Josh Porter of Bokardo, and the world according to Google says that 143,000 folks are...

Read more about Build your very own seat at the strategy table...

Whose cloud is it anyway?


We were invited to demonstrate our Referral Management Solution at Friday's Cloud Computing Roundtable, put on by TechCrunch. The provocative title of the event was "Whose cloud is it anyway?", and TechCrunch invited a who's who in cloud computing...

Read more about Whose cloud is it anyway? ...