PHP Extension Dynamics CRM: Create an Account

Back to Index

Creates a new account.

Documentation: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/webapi/create-entity-web-api

CURL Command

curl -X POST https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/accounts \
  -H "Accept: application/json" \
  -H "Content-Type: application/json; charset=utf-8" \
  -H "OData-MaxVersion: 4.0"  \
  -H "OData-Version: 4.0" \
  -H "Authorization: Bearer DYNAMICS_CRM_ACCESS_TOKEN" \
  -d '{
    "name": "Sample Account",
    "creditonhold": false,
    "address1_latitude": 47.639583,
    "description": "This is the description of the sample account",
    "revenue": 5000000,
    "accountcategorycode": 1
}'

PHP Extension Example

<?php

// The version number (9_5_0) should match version of the Chilkat extension used, omitting the micro-version number.
// For example, if using Chilkat v9.5.0.48, then include as shown here:
include("chilkat_9_5_0.php");

$rest = new CkRest();

//  URL: https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/accounts
$bTls = true;
$port = 443;
$bAutoReconnect = true;
$success = $rest->Connect('my-dynamics-domain.api.crm.dynamics.com',$port,$bTls,$bAutoReconnect);
if ($success != true) {
    print 'ConnectFailReason: ' . $rest->get_ConnectFailReason() . "\n";
    print $rest->lastErrorText() . "\n";
    exit;
}

$json = new CkJsonObject();
$json->UpdateString('name','Sample Account');
$json->UpdateBool('creditonhold',false);
$json->UpdateNumber('address1_latitude','47.639583');
$json->UpdateString('description','This is the description of the sample account');
$json->UpdateNumber('revenue','5000000');
$json->UpdateNumber('accountcategorycode','1');

$rest->AddHeader('Content-Type','application/json; charset=utf-8');
$rest->AddHeader('OData-Version','4.0');
$rest->AddHeader('Accept','application/json');
$rest->AddHeader('OData-MaxVersion','4.0');
$rest->AddHeader('Authorization','Bearer DYNAMICS_CRM_ACCESS_TOKEN');

$sbRequestBody = new CkStringBuilder();
$json->EmitSb($sbRequestBody);
$sbResponseBody = new CkStringBuilder();
$success = $rest->FullRequestSb('POST','/api/data/v9.0/accounts',$sbRequestBody,$sbResponseBody);
if ($success != true) {
    print $rest->lastErrorText() . "\n";
    exit;
}

$respStatusCode = $rest->get_ResponseStatusCode();
if ($respStatusCode >= 400) {
    print 'Response Status Code = ' . $respStatusCode . "\n";
    print 'Response Header:' . "\n";
    print $rest->responseHeader() . "\n";
    print 'Response Body:' . "\n";
    print $sbResponseBody->getAsString() . "\n";
    exit;
}


?>