PHP Extension Dynamics CRM: Lookup Contact ID using Full Name

Back to Index

Find a contactid given a fullname.

Documentation: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/web-api/contact?view=dynamics-ce-odata-9

CURL Command

curl -X GET https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/contacts
  -H "Accept: application/json" \
  -H "OData-MaxVersion: 4.0"  \
  -H "OData-Version: 4.0" \
  -d "$select=contactid" \
  -d "$filter=fullname eq 'Tomasz Bochenek'" \
  -H "Authorization: Bearer DYNAMICS_CRM_ACCESS_TOKEN"

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/contacts
$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;
}

$rest->AddQueryParam('$select','contactid');
$rest->AddQueryParam('$filter','fullname eq 'Tomasz Bochenek'');

$rest->AddHeader('OData-MaxVersion','4.0');
$rest->AddHeader('Accept','application/json');
$rest->AddHeader('OData-Version','4.0');
$rest->AddHeader('Authorization','Bearer DYNAMICS_CRM_ACCESS_TOKEN');

$sbResponseBody = new CkStringBuilder();
$success = $rest->FullRequestNoBodySb('GET','/api/data/v9.0/contacts',$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;
}

$jsonResponse = new CkJsonObject();
$jsonResponse->LoadSb($sbResponseBody);

$odataContext = $jsonResponse->stringOf('\'@odata.context\'');
$i = 0;
$count_i = $jsonResponse->SizeOfArray('value');
while ($i < $count_i) {
    $jsonResponse->put_I($i);
    $odataEtag = $jsonResponse->stringOf('value[i].\'@odata.etag\'');
    $contactid = $jsonResponse->stringOf('value[i].contactid');
    $i = $i + 1;
}


?>

Sample JSON Response Body

{
  "@odata.context": "https://mydomain.api.crm.dynamics.com/api/data/v9.0/$metadata#contacts(contactid)",
  "value": [
    {
      "@odata.etag": "W/\"1162210\"",
      "contactid": "1fa1e5b9-88df-e311-b8e5-6c3be5a8b200"
    }
  ]
}