Der Code für die Anbindung - hier in PHP
<?php
header("Content-Type: application/json");
// Konfiguration
$tenantId = "b324339d-30ef-442a-8529-b8afb86dcae6";
$clientId = "2b20a5b6-780a-4441-aaac-7918a95d89ef";
$clientSecret = "I9S8Q~nNohO2DN0Nu4xWhWUig_gYSKYenqHMxcSV";
$resource = "https://org09595ab5.crm16.dynamics.com/";
$dataverseUrl = $resource . "/api/data/v9.2";
// Eingehende JSON-Daten lesen (JSON ist ein Datenformat ähnlich wie z.B. CSV)
$input = json_decode(file_get_contents("php://input"), true);
$firstname = $input["firstname"] ?? "";
$lastname = $input["lastname"] ?? "";
$email = $input["email"] ?? "";
$phone = $input["phone"] ?? "";
$zaehlernummer = $input["zaehlernummer"];
// Funktion zum holen eines Tokens
// definieren - Authentifizierung
function getAccessToken($tenantId, $clientId, $clientSecret, $resource) {
$url = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token";
$data = [
"client_id" => $clientId,
"client_secret" => $clientSecret,
"scope" => "$resource/.default",
"grant_type" => "client_credentials"
];
$options = [
"http" => [
"header" => "Content-Type: application/x-www-form-urlencoded\r\n",
"method" => "POST",
"content" => http_build_query($data),
],
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) {
return null;
}
return json_decode($result, true)["access_token"];
}
// Funktion für den API Request definieren
// Hier könnten Ergänzungen für die Fehlerbehandlung
// z.B. bei doppelten Datensätzen, unvollständigen Daten etc.
// integriert werden
function callDataverseAPI($url, $token, $method, $body = null) {
$headers = [
"Authorization: Bearer $token",
"Content-Type: application/json",
"Accept: application/json"
];
$options = [
"http" => [
"header" => implode("\r\n", $headers) . "\r\n",
"method" => $method,
"content" => $body ? json_encode($body) : null,
"ignore_errors" => true
]
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$responseHeaders = $http_response_header ?? [];
return ["headers" => $responseHeaders, "body" => $result];
}
// Hier beginnt der eigentliche Datenaustausch
//
// 1. Token holen (Authentifizierung & Autorisierung)
$token = getAccessToken($tenantId, $clientId, $clientSecret, $resource);
// Fehler ausgeben wenn nicht erfolgreich
if (!$token) {
echo json_encode(["success" => false, "error" => "Token konnte nicht geholt werden"]);
exit;
}
// 2. Kontakt anlegen
// Daten stammen aus dem Formular
$contactData = [
"firstname" => $firstname,
"lastname" => $lastname,
"emailaddress1" => $email,
"telephone1" => $phone,
//"cr1fb_fiktivezaehlernummer" => $zaehlernummer
];
$contactResponse = callDataverseAPI("$dataverseUrl/contacts", $token, "POST", $contactData);
// GUID aus Header ziehen
$contactUri = "";
foreach ($contactResponse["headers"] as $header) {
if (stripos($header, "OData-EntityId:") !== false) {
$contactUri = trim(str_replace("OData-EntityId:", "", $header));
break;
}
}
if (!$contactUri) {
echo json_encode(["success" => false, "error" => "Kontakt konnte nicht erstellt werden", "content" => $contactResponse]);
exit;
}
// 3. Incident anlegen
$incidentData = [
"title" => "Neuer Kunde – Begrüßungsfall",
"description" => "Erster Fall für diesen neuen Kunden.",
"caseorigincode" => 2, // E-Mail
"prioritycode" => 2, // Normal
"customerid_contact@odata.bind" => str_replace($dataverseUrl, "", $contactUri)
];
$incidentResponse = callDataverseAPI("$dataverseUrl/incidents", $token, "POST", $incidentData);
// Incident GUID auslesen
$incidentUri = "";
foreach ($incidentResponse["headers"] as $header) {
if (stripos($header, "OData-EntityId:") !== false) {
$incidentUri = trim(str_replace("OData-EntityId:", "", $header));
break;
}
}
if (!$incidentUri) {
echo json_encode(["success" => false, "error" => "Incident konnte nicht erstellt werden"]);
exit;
}
// 4. Erfolg zurückgeben
echo json_encode([
"success" => true,
"customer" => "$firstname $lastname",
"contactUri" => $contactUri,
"incidentId" => $incidentUri
]);