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.

image

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_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
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));
curl_close($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 login.live.com, as below:


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

and

$securityTokenXML = LiveIDManager::GetSOAPResponse("/liveidSTS.srf" , "login.live.com" , "https://login.live.com/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 = "urn:crm:dynamics.com";

if (strpos($CRMUrl,"crm4.dynamics.com")) {
$URNAddress = "urn:crm4:dynamics.com";
}

if (strpos($CRMUrl,"crm5.dynamics.com")) {
$URNAddress = "urn:crm5:dynamics.com";
}

$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
crm:dynamics.comcrmna:dynamics.com
crm4:dynamics.comcrmemea:dynamics.com
crm5:dynamics.comcrmapac:dynamics.com

Conclusion


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

65 comments:

Anonymous said...

Hello. I've tried to correct the files from CRM Kit but with no success.
Could you send me please your solution to pertld[at]seznam.cz?

Thank you.

Anonymous said...

Hi Tanguy,
Thanks a lot for your solution. That's work perfectly with a standard online account. However, it is not working with new office 365 dynamics CRM account, I can log a security issue. Did you try this ? It is ODPS connection I guess.
Thanks for your reply.

Tanguy said...

This code is made for 0ffice 365 account... It's strange it is not working for you...

What is the exact error message you get?

Steve Rooke said...

Tanguy,

This solved the problem for us - great solution, just wondering why this is available via you rather than Microsoft?!

Tanguy said...

There is a blog post form a MS guy that explain how to communicate with CRM Online using SOAP calls. It is the guy that originally wrote the lab in CRM Developer kit but he is not in the same team anymore. That's why the content is not updated

Anonymous said...

Hello!

Thanks for the post!

You wrote this is for the new authentication model in CRM. You mean the Office 365 authentication?

We still use Windows Live Id authentication. Anyway, the original Microsoft training code doesn't work.

Do you have any idea why that can be? Do I have to change something?

I tried your updated code, but it doesn't work either.

Thanks
Tamas

Tanguy said...

Yes, I mean Office 365 authentication.

What I've done to be sure SOAP request was correct is to run fiddler while I'm connecting to my CRM Online organization with one of my tools.

Then I analyzed each soap request to be sure the ones in the lab was correct.

I think I had to change one or two stuffs in the requests

Anonymous said...

Dear it doesn't work for me...


1.
curl_setopt($cURLHandle, CURLOPT_SSLVERSION , 3);

2.
replace “/liveidSTS.srf” with “/extSTS.srf” in $binaryDATokenXML and $securityTokenXML

and
3.
replace the followin
Old value New value
crm:dynamics.com crmna:dynamics.com
crm4:dynamics.com crmemea:dynamics.com
crm5:dynamics.com crmapac:dynamics.com


but still it is not authenticating with vrm user, but it works with live id..even if it authenticate with live id can do anything with crm... please help me asap...

Anonymous said...

Hallo dear!

please tell me if what i have done is wrong.. now with the changes you asked me to do, i cant even login with Live id or crm id.


my url
$organizationServiceURL = "https://(myownname).api.crm.dynamics.com/XRMServices/2011/Organization.svc";

changes as i did
1.

$cURLHandle = curl_init();
curl_setopt($cURLHandle, CURLOPT_URL, $soapUrl);
curl_setopt($cURLHandle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cURLHandle, CURLOPT_TIMEOUT, 60);
curl_setopt($cURLHandle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($cURLHandle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
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));
curl_close($cURLHandle);

2.
$binaryDATokenXML = LiveIDManager::GetSOAPResponse("/extSTS.srf" , "login.live.com" , "https://login.live.com/extSTS.srf", $soapTemplate);

$securityTokenXML = LiveIDManager::GetSOAPResponse("/extSTS.srf" , "login.live.com" , "https://login.live.com/extSTS.srf", $securityTemplate);

3.
$URNAddress = "crm:dynamics.com";

if (strpos($CRMUrl,"crm4.dynamics.com")) {
$URNAddress = "crmemea:dynamics.com";
}

if (strpos($CRMUrl,"crm5.dynamics.com")) {
$URNAddress = "crmapac:dynamics.com";
}

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


here are error i get
Notice: Undefined offset: 1 in F:\Projects\PHP2CRMonline\LiveIDManager.php on line 59

Warning: DOMDocument::loadXML(): Empty string supplied as input in F:\Projects\PHP2CRMonline\LiveIDManager.php on line 140
Unable to authenticate LiveId.

please reply to me asap..

thank you very much for your kind help

Tanguy said...

Give me your email address, I'll send you my files

Saint Noozan Ali said...
This comment has been removed by the author.
Saint Noozan Ali said...


please send me whole project files
my email is:

noozaan@hotmail.com

thank you very much

Saint Noozan Ali said...

dear sir

when can you send me the project files.

i will be very thankful if you can send me before Sunday as i have to finish the project by monday...

send to my hotmail or gmail account
noozaan@hotmail.com
noozaan@gmail.com

Saint Noozan Ali said...

i have been doing all research about this and this is what i found in Microsoft site... its in C# and end point is totally diff. from yours..
STSEnpoint = "https://login.microsoftonline.com/RST2.srf";


here are microsoft link about that..

[code]http://code.msdn.microsoft.com/CRM-Online-2011-WebServices-14913a16

ps. even this is not fully working for me..
thank you and waiting for you to mail the project

Unknown said...

Thanks Ali for the link. I copied the authentication parameters from the C# code from there. Now the PHP code works perfectly. So, what I have done, I changed the URN address to urn:crmemea:dynamics.com and changed all the liveidSTS.srf values to extSTS.srf. I can suggest to make a search in your PHP project to find all the values which you have to change, cause it occurs on different places.

I am using LiveId authentication for our CRM Online.

Thanks to Tanguy again for the code.
Though I am still curious, do I have to write all the code for the CRUD operations? I want to retrieve and edit Contact data and it seems it is time wasting to implement it myself.

thx,
Tamas

Tanguy said...

For SOAP access, yes, you have to code by your own all CRUD operations...

But you should be able to factorize them since all SOAP requests are similar (except operation name and data content)

Anonymous said...

Noozan Ali here
please send me php coding for crm online office 365. Also my login is not live id .. my email is noozaan@hotmail or gmail.com

Anonymous said...

I have been trying for so long to get php to talk to crm 2011 online. please send me your project, will be eternally grateful!

email is: superfly007@outlook.com

Anonymous said...

well, i got the connection thing all sorted, but i am stuck on setting an OptionSetValue when creating a new lead This is in my sop request body:

leadsourcecode 3

but it fails with:

The

deserializer has no knowledge of any type that maps to this name. Consider
changing the implementation of the
ResolveName method on your
DataContractResolver to return a
non-null value for name
'industrysourcecode' and
namespace ''.'.


Any ideas?

thanks

Raffaello Foldes said...

Hi
we tried all your suggestion but it still doen't work.
We are trying to connect to a CRM online via php, with o.365 login on a emea server.
Do you have something we can try directly?
Regards
Raffaello

Anonymous said...

Tanguy,

Thank you very much for this information! Got it working now.

Muhammad Sohail said...
This comment has been removed by the author.
Muhammad Sohail said...

i used PHP to CRM Lab but that changes not work for me so plz send me update solution code file for Office365 crm online
Mail:sohail.libra786@gmail.com

Hash innvotech said...

as confirmation we wanna develope Cloud Based CRM which tools are most prefrable...and please send also files whatever related about on
anilp80u@hotmail.com

_ _ said...

I just came across your post and I think I've done everything correctly but am still getting "Unable to authenticate LiveId." when trying to run the example from the Developer kit. I'm not sure if the issue is something I've missed, I have the wrong account type or things have changed since your post but, I would really appreciate if you could send me your working files. Thanks!

ruben [at] redlotusaustin.com

Saboor Awan said...

i have facing the same issue,

Unable to authenticate LiveId.

if it works for someone, please send me the updated code at Sabor_11@yahoo.com

Tanguy said...

Hi,
The mechanism has changed again... So this article is no longer valid.

When (and if) I have time, I will post a new solution

Roshu said...

If you post the new solution would be great, I really need it, as I am getting auth errors while trying to log in.

Thanks

Matthew said...

An article on how to do it now Microsoft have changed the authentication would also be massively useful for me :)

Anonymous said...

I have the same issue. Could you please send the files to me? My email is yang.qianming@gmail.com.

Thanks

Kevin Reed said...

Check out this post for details on XML that works for me to create leads in CRM from php:
http://social.microsoft.com/Forums/en-US/crm/thread/1e148175-c715-4af2-b087-7ce8a28a809b

Anonymous said...

Hello everyone, I've been looking for 2 weeks to find a way to do CRUD for the CRM dynamics 2011, i got nothing yet, i'm not an expert that much, i tried almost everything i could find on the internet, has someone been able to reach a solution for that?

Anonymous said...

You should never ever use the following line like this:
curl_setopt($cURLHandle, CURLOPT_SSL_VERIFYPEER, false);

This is because this leaves you open to a man in the middle attack, since you are not verifying the certificate that you receive from the server. So basically anyone can intercept the connection and read (and change!) your data.

Look here how to fix that:
http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/

Anonymous said...

Hi all

it is not working for me any buddy provide me file which workinf for office 365

Anonymous said...

i am using it with the old structure i can get the authetication token but when i try to create account i get this error

http://www.w3.org/2005/08/addressing/soap/faulturn:uuid:65982f6aa-c8a8-495c-9c42-ee8ea3a8bda4s:Sendera:FailedAuthenticationID3242: The security token could not be authenticated or authorized.

manumazu said...

Hello,
Same problem for me: when i try to querying CRM i have this error : "ID3242: The security token could not be authenticated or authorized". However the identification seems to be good because security token are well recovered. It's strange that MS doesn't give any solution for PHP interface. Hope it will change ...

Anonymous said...

All instances of the login URL need to be https://login.microsoftonline.com/extSTS.srf

After changing the domain as well as following your steps it worked for me using Office 365.

Anonymous said...

Success!

Using Office 365.

1) I followed all the steps outlined by Tanguy (the author of this post)

2) I changed all instances of login.live.com to login.microsoftonline.com as suggested in a recent comment

3) When it still didn't work I pulled my head out of my ass and changed the value of $liveIDUseranme and $liveIDPassword to a valid username and password and then everything worked fine

Priya sen said...

Hi,
I have facing the same issue,

Unable to authenticate LiveId.
I have made above mention changes
in my files.

if it works for someone, please send me the updated code at priya124sen@gmail.com

Anonymous said...

Just as an aside, I'm fairly certain my shared hosting providers IP got blocked by Microsoft. if it all of a sudden stops working you might want to check from a different server.

Anonymous said...

Great article! I went through the same steps, and unfortunately it didn't quite work. But then I did what the message from Anonymous on June 19, 2013 at 12:50 AM said to do, and it worked for me! I was having all the same problems that others were, and now I'm able to communicate with CRM. Thanks!

Paul Tsang said...

I'm looking for the solution to access MSCRM 2011 by PHP for a long time. I've been a hard time to code it byt fail. Can someone kindly send me a sample php code for do that? My email address is paulpaulpaulgood@gmail.com

Many many many thanks!!!

rajesh said...

After working in this for a lot of time, I got this to work. I hosted my working sample at : https://github.com/epinapala/MsDynamicsPHP

hope this helps others.

Roshu said...

I managed to make the code work, so I can login only by doing the changes the author said. BUt I cant CRUD the entities in the CRM , i get the "Access is denied" response. Can someone help me out regarding this issue ?

Anonymous said...

I loved your post THANK YOU!! I even had Microsoft Support give me the link to this article as a solution to helping me solve this issue, honestly they are useless and wasted three days and long and short I am back where I started, with you.

I did manage to get the code to give me the tokens and the ciphers, but can not get any CRUD functionality - just as Roshu and others have mentioned.

I get the same "Access Denied". Using postman I get "Invalid STS". I am about to start creating a wrapper in c#, I really do not want to do that...

Does anyone have PHP code that can connect and perform CRUD operations for CRM 2013 Online with 365, or maybe I could get a copy of this code as well, pretty please with sugar on top. :)

shellypublic@outlook.com

Thanks for any help!

Anonymous said...

Hey Guys,
I am having the same problem. I am using the class at https://code.google.com/p/php-dynamics-crm-2011/

But it gives lot of error, not sure what the error is.

What I want is I want to create a new lead in the CRM using php. I want to push the leads in my database to ms dynamic crm. my email: mebishwadeep@gmail.com

Vinod Nardiya said...

Can anybody send me the updated code for ms crm online authentication with windows live id. My email id vnardiya.eng@gmail.com

Omega software said...

Keep up the fantastic piece of work, I read few blog posts on this web site and I believe that your site is real interesting and has lots of great information. ERP Software in Mumbai || System Software || CRM Software in Mumbai || MLM Software

Omega software said...

I really appreciate spending some time to talk about that, I believe firmly regarding this and so really enjoy understanding more about this kind of subject.This is also a very good post which I really enjoyed reading. It is not everyday that I have the possibility to see something like this. CRM Software || MLM Software in Mumbai || ERP Software || System Software in Mumbai

Afsar Khan said...

I want it urgently.I am in very need.Please update it or send me on afsarkhan10182@gmail.com

Anonymous said...

Hello
please help me.
i have error :SSL connect errorSSL connect error Warning: DOMDocument::loadXML(): Empty string supplied as input in /home/ibisweb/www/https/home/phpcrm/LiveIDManager.php on line 138 Unable to authenticate LiveId.

please send : nguyen.thi.thu.phi@quantic.com.vn

axl echizen said...

Hello Tanguy. please send me also the file.. I really really need it right now. :( thank you.

email: axlechizen@gmail.com

Shiwani Malhotra said...

Fahimshakir Freelancer Developer from Delhi-India,You can provide me part time work for Home,Part Time Developer/Freelancer For PHP , WordPress, Magento, Opencart, Shopify, Codeigniter, Website Maintenance in Delhi -india, www.fahimshakir.com

Skill Quotient said...

This post has a lot of importance to the people and I got some useful information from your blog. I hope you can continue to inspire and post more of this.
Microsoft Dynamics CRM Training | Microsoft Dynamics CRM Online Training

susmita maity said...

can anyone send me his/her working php code at susmitamaity13@gmail.com

Thomas W. said...

Hi Tanguy,

good work ever.

I would like to get it running in CRM 2015 IFD-Enviroments with ADFS.

Can you advise, what has to be done and give some snippets?

Thomas

axl echizen said...

Hi Tanguy,

I followed you instructions above, the authentication worked but the insert of the new record directly in crm dynamics is not working. Please help me in my issue.

Here's my email address.

Email: axlechizen@gmail.com

Thank you so much.

KingRiderBR said...

Thanks rajesh, working a git epinapala/MsDynamicsPHP

Anonymous said...

Hi Tanguy,
I am trying to connect crm2011 through your project. I have followed all step which you mentioned still i m getting error "Empty string supplied as input in LiveIDManager.php on line 140". Please help me what would be the cause. I have been struggling in crm integration from last 1 month still not getting any success. My mail id is ajit.chavhan@gmail.com . Please help me ASAP.

Anonymous said...

Hi Tanguy,
I am trying to connect crm2011 through your project. I have followed all step which you mentioned still i m getting error "Empty string supplied as input in LiveIDManager.php on line 140". Please help me what would be the cause. I have been struggling in crm integration from last 1 month still not getting any success. My mail id is ajit.chavhan@gmail.com . Please help me ASAP.

Anonymous said...


Hello all,
I want to create lead from php page that will be show
In Microsoft dynamic Crm 2015.
Mailid: akram.khan@dotsquares.com
Help me.
Thank in advance

Girish Sharma said...
This comment has been removed by the author.
Girish Sharma said...

Hello all,

I have implemented these code but have following errors

Notice: Undefined offset: 1 in /home/ds07/public_html/helpinghands/php2crm/LiveIDManager.php on line 60

Warning: DOMDocument::loadXML(): Empty string supplied as input in /home/ds07/public_html/helpinghands/php2crm/LiveIDManager.php on line 142
Unable to authenticate LiveId.

thanks in advance

Ramya Reddy said...

Iam very much intrested to know about this online training.Thanks for the informative article.Can you kindly provide SAP Fiori online access


Mohammed Abulsoud said...

How authenticate CRM 2016 online that is protected by ADFS?