Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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', '[email protected]');
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.

like image 325
Ajin Avatar asked May 19 '26 04:05

Ajin


1 Answers

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.

like image 94
Polshgiant Avatar answered May 20 '26 18:05

Polshgiant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!