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_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);
$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 value | New value |
crm:dynamics.com | crmna:dynamics.com |
crm4:dynamics.com | crmemea:dynamics.com |
crm5:dynamics.com | crmapac: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
Comments
Could you send me please your solution to pertld[at]seznam.cz?
Thank you.
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.
What is the exact error message you get?
This solved the problem for us - great solution, just wondering why this is available via you rather than Microsoft?!
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
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
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...
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
please send me whole project files
my email is:
noozaan@hotmail.com
thank you very much
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
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
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
But you should be able to factorize them since all SOAP requests are similar (except operation name and data content)
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
email is: superfly007@outlook.com
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
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
Thank you very much for this information! Got it working now.
Mail:sohail.libra786@gmail.com
anilp80u@hotmail.com
ruben [at] redlotusaustin.com
Unable to authenticate LiveId.
if it works for someone, please send me the updated code at Sabor_11@yahoo.com
The mechanism has changed again... So this article is no longer valid.
When (and if) I have time, I will post a new solution
Thanks
Thanks
http://social.microsoft.com/Forums/en-US/crm/thread/1e148175-c715-4af2-b087-7ce8a28a809b
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/
it is not working for me any buddy provide me file which workinf for office 365
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.
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 ...
After changing the domain as well as following your steps it worked for me using Office 365.
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
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
Many many many thanks!!!
hope this helps others.
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!
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
email: axlechizen@gmail.com
Microsoft Dynamics CRM Training | Microsoft Dynamics CRM Online Training
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
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.
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.
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.
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
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