web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

PHP Connect to Microsoft Dynamics CRM

(0) ShareShare
ReportReport
Posted on by 5
Hello, 

I am trying to connect to Microsoft Dynamics CRM using an Application or Client Id and a Client Secret based authentication. I have client id, client secret and Tenant Id.

But it does not seem to connect, I get the next error: "AADSTS90002: Tenant 'xxx-xxx-xxx-xxx-xxx' not found. Check to make sure you have the correct tenant ID and are signing into the correct cloud." eventhought the client claims that the tenant id is correct, so I am guessing I am doing something wrong here.

Here is the code:

$clientId = 'xxx-xxx-xx-xx-xx';
$clientSecret = 'test';
$resource = 'test.crm4.dynamics.com';
$tokenEndpoint = 'login.microsoftonline.com/.../token';

// Prepare the request body
$params = array(
    'grant_type' => 'client_credentials',
    'client_id' => $clientId,
    'client_secret' => $clientSecret,
    'resource' => $resource
);
$query = http_build_query($params);

// Create the cURL request
$ch = curl_init($tokenEndpoint);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the request
$response = curl_exec($ch);
curl_close($ch);

// Parse the response
$data = json_decode($response, true);
print_r($response);

Then i should get the leads from the crm:

if (isset($data['access_token'])) {
    $accessToken = $data['access_token'];

    // Use the access token to make API requests
    // For example, retrieve leads
    $leadsEndpoint = 'test.crm4.dynamics.com/.../leads';
    $headers = array(
        'Authorization: Bearer ' . $accessToken,
        'Accept: application/json',
        'OData-MaxVersion: 4.0',
        'OData-Version: 4.0',
    );

    $ch = curl_init($leadsEndpoint);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

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

    // Process the leads response
    $leads = json_decode($response, true);
    foreach ($leads['value'] as $lead) {
        // Process each lead record as needed
        $leadId = $lead['leadid'];
        $fullName = $lead['fullname'];
        $email = $lead['emailaddress1'];

        echo "Lead ID: $leadId\n";
        echo "Full Name: $fullName\n";
        echo "Email: $email\n";
        echo "\n";
    }
} else {
    // Handle authentication error
    if (isset($data['error_description'])) {
        echo "Authentication Error: " . $data['error_description'];
    } else {
        echo "Authentication Error";
    }
}

I do not understand what I am doing wrong, there are just a few examples on the internet. I have also tried Alexa CRM, but my php version is not suitable, as I cannot upgrade it because of the other projects on the server.

Please excuse my English, I am not a native English person.

Please help! Thank you!

I have the same question (0)
  • Suggested answer
    slavuflorina Profile Picture
    5 on at

    Ok, so the problem was the tenant id.

    They gave me the wrong one.

    It works great!

    Thank you!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans