简体   繁体   中英

Microsoft Dynamics CRM and PHP/JSON

I have to implement form on a webpage, that sends data to Microsoft Dynamics CRM when submited. The data needs to be saved to a certain lead.

I have created simple PHP script that uses curl to communicate with CRM server but I always get 401 status code that indicates authorization has failed.

define('MS_CRM_URL', 'https://______.crm.dynamics.com/');
define('MS_CRM_USER', 'user@domain.com');
define('MS_CRM_PASS', 'password');
$method = '/api/data/v8.0/accounts?$select=name&$top=3';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, MS_CRM_URL . $method);
curl_setopt($ch, CURLOPT_USERPWD, MS_CRM_USER .':'. MS_CRM_PASS);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(

));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Accept' => 'application/json',
    'OData-MaxVersion' => '4.0',
    'OData-Version' => '4.0',
    'Content-Type' => 'application/json',
));

$server_output = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

$json = array();
if ((int)$status_code === 200) {
    $json = json_decode($server_output);
}

echo '<pre>';
var_dump($status_code);
var_dump($json);
echo '</pre>';

The $method var contents are taken from an example that I've found somewhere on Microsoft documentation site. The documentation was not very helpful to me.

Have you seen this yet? http://jlattimer.blogspot.com/2015/02/soap-only-authentication-using-php.html

Also, it looks like there's an ADAL library for PHP. https://github.com/jamesmcq/azure-activedirectory-library-for-php You should be able to authenticate using that.

You can do it using SOAP Authentication. I have same requirement where we need to send data to Dynamics 365 online from PHP website. I have achieved it using source code from below link :

https://bitbucket.org/nigelheap/msdynamicsphp-master

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