简体   繁体   中英

PHP curl talking to Microsoft Dynamics CRM 2011

I am trying to pull some data from Microsoft Dynamics CRM 2011 Online, which resides on remote host; it works if I manualy (from browser) log in and exec my from my server which runs PHP on Linux host, but of course i want to authenticate and run CRUD operations without my mediation :) .

But it keeps on showing me "Object moved to here." page for authentication. If i set CURLOPT_FOLLOWLOCATION => 1, it shows login page of crm.

If anyone can suggest any clue for resolving situation... thanks!

<?php

$useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
$urlValue  = "/LeadSet?$select=Address1_City,FirstName,LastName";

$username  = "domain\user";
$pass      = "pass";


$handle = curl_init();    
curl_setopt_array($handle, 
        array (
            CURLOPT_USERAGENT => $useragent,
            CURLOPT_USERPWD   => $username . ':' . $pass,
            CURLOPT_HTTPAUTH  => CURLAUTH_ANY,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_URL  => 'https://myhost.com/xrmservices/2011/OrganizationData.svc',
            CURLOPT_POST => 1,
            CURLOPT_POSTFIELDS     => $urlValue,
            CURLOPT_RETURNTRANSFER => true,
        )
);

$response = curl_exec($handle);
curl_close($handle);

header('Content-Type: text/plain;');
print_r($response);

I solve da problem make an authentication by ntml, this code shows how to retrieve contacts from CRM in JSON format.

$url='http://<server_adress>/organizationName/XRMServices/2011/OrganizationData.svc/ContactSet';
$ch = curl_init();
 $headers = array(
      'Method: GET',
      'Connection: keep-alive',
      'User-Agent: PHP-SOAP-CURL',
      'Content-Type: application/json; charset=utf-8',
      'Accept: application/json',
      'Host <server network adress>');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_setopt($ch, CURLOPT_USERPWD, '<user>:<password>');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
//echo $response; 
$array=json_decode($response, true);
$size=count($array['d']['results']);
echo "Numero de registos:".$size."<p>";
echo "Contactos:<p>";
for($i=0;$i<$size;$i++){
 echo "Nome: ". $array['d']['results'][$i]['FullName']."<p>";
 echo "Cidade: ". $array['d']['results'][$i]['Address1_City']."<p>";
 echo "Emprego: ". $array['d']['results'][$i]['JobTitle']."<p>";
 echo "Morada: ". $array['d']['results'][$i]['Address1_Name']."<p>";
 echo "Telefone: ". $array['d']['results'][$i]['Address1_Telephone1']."<p>";
 echo "Email: ". $array['d']['results'][$i]['EMailAddress1']."<p>";
 echo "Pais: ". $array['d']['results'][$i]['Address1_Country']."<p>";
 echo "Codigo-postal: ". $array['d']['results'][$i]['Address1_PostalCode']."<p>";
 echo "Criado por: ". $array['d']['results'][$i]['CreatedBy']['Name']."<p>";
 echo "...................................................................................<p>";       
} 

If somebody knows how to make crud operations in rest endpoint please tell me.

I am afraid that i have answer to my own question, what i am trying to achive is not possible, because of this: Authentication is only possible within the application .

As noted here:

http://msdn.microsoft.com/en-us/library/gg334279

And i am trying to authenticate from remote site.

Does adding the following CURL option help?

CURLOPT_HTTPAUTH => CURLAUTH_NTLM

It did the trick for me. Apparantly, the server to which I am connecting does not support basic HTTP authentication but enforces NTLM. Might help in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM