Search This Blog

Friday, August 17, 2012

PHP to CRM Online : An easy way to do

Last week, for two of my customers, I was asked to find the way to communicate with CRM Online using PHP. I don’t know anything about PHP so it was quite a challenge to do it.


Nevertheless, Microsoft provide some help with the CRM Developer training kit (download it here). It contains a sample to connect to CRM Online with PHP (The lab is named “CRM Online from PHP”) but this sample is out of date since Microsoft changed the authentication model in Microsoft Dynamics CRM Online.

Here is the changes to perform in order to make this sample working.

Use of SSL v3

First thing is to enable use of SSL v3 in the PHP sample. To do this, open the file “LiveIDManager.php” and find the method GetSOAPResponse. This method uses Curl (Client URL Request Library) to perform url calls. Add the following lines before the call of method “curl_exec”:

curl_setopt($cURLHandle, CURLOPT_SSLVERSION , 3);

The final code for the curl use should be like below

$cURLHandle = curl_init();
curl_setopt($cURLHandle, CURLOPT_URL, $soapUrl);
curl_setopt($cURLHandle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cURLHandle, CURLOPT_TIMEOUT, 180);
curl_setopt($cURLHandle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($cURLHandle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($cURLHandle, CURLOPT_POST, 1);
curl_setopt($cURLHandle, CURLOPT_POSTFIELDS, $content);
curl_setopt($cURLHandle, CURLOPT_SSLVERSION , 3);

$response = curl_exec($cURLHandle);
echo (curl_error($cURLHandle));

Use of new endpoint for authentication

Still in LiveIDManager.php, the method “authenticateWithLiveID” retrieves authentication token based on the Microsoft account specified (formerly known as Live Id). There is two calls to, as below:

$binaryDATokenXML = LiveIDManager::GetSOAPResponse("/liveidSTS.srf" , "" , "", $soapTemplate);


$securityTokenXML = LiveIDManager::GetSOAPResponse("/liveidSTS.srf" , "" , "", $securityTemplate);

If your organization uses the new authentication model, then the endpoint to use is not “/liveidSTS.srf” but “/extSTS.srf

You will also find the code below that select the Endpoint reference for CRM Online depending on the Url specified for the organization.

$URNAddress = "";

if (strpos($CRMUrl,"")) {
$URNAddress = "";

if (strpos($CRMUrl,"")) {
$URNAddress = "";

$securityTemplate = sprintf(
$securityTokenSoapTemplate, LiveIDManager::gen_uuid(), LiveIDManager::getCurrentTime(), LiveIDManager::getNextDayTime(), $liveIDUsername, $liveIDPassword, $cipherValue, $URNAddress);

these “urn” are no more used in the new authentication model for CRM Online. Here is the new values you have to use:

Old valueNew value


With few updates on the lab from CRM Developer toolkit, you will now be able to communicate with CRM Online from PHP