
1. Recupérer les informations digest
function http_auth($realm,$auth_digest)
{
if (empty($_SERVER['PHP_AUTH_DIGEST']))
{
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Digest realm="'.$realm.'",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"');
$needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1, 'opaque' => 1);
$data = array();
$keys = implode('|', array_keys($needed_parts));
preg_match_all('@(' . $keys . ')=(?:([\'"])([^\2]+?)\2|([^\s,]+))@', $auth_digest, $matches, PREG_SET_ORDER);
foreach ($matches as $m)
{
$data[$m[1]] = $m[3] ? $m[3] : $m[4];
unset($needed_parts[$m[1]]);
}
return $needed_parts ? false : $data;
}
}
$realm = 'API_KEY';
$auth_digest = $_SERVER['PHP_AUTH_DIGEST'];
$authApi = http_auth($realm,$auth_digest);
2. Génération de la réponse au serveur
$username = 'USERNAME';
$pw = "PASSWORD";
$nonce = $authApi['nonce'];
$qop = $authApi['qop'];
$uri = 'beneficiaire/create/';
$opaque = $authApi['opaque'];
$nc = 00000001;
$cnonce=1;
$A1 = md5($username . ':' . $realm . ':' . $pw);
$A2 = md5('PUT:'.$uri);
$valid_response = md5($A1.':'.$authApi['nonce'].':'.$nc.':'.$cnonce.':'.$authApi['qop'].':'.$A2);
$url = 'https://api.applewood-stim.fr/beneficiaire/create';
$headers = [
'Content-Type: application/json',
'Authorization: Digest
username="' . $username . '",
realm="' . $realm . '",
nonce="' . $nonce . '",
uri="beneficiaire/create/",
cnonce="'. $cnonce .'",
nc='. $nc .',
qop="' . $qop . '",
response="' . $valid_response . '",
opaque="' .$opaque . '"',
'Host: api.applewood-stim.fr'
];
$data = ['email' => 'test@test.fr']; //email : obligatoire, informations compléementaires : code_client, nom, prenom ...
3. Requete CURL
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
$result = curl_exec($curl);
$output = json_decode($result, true);
return $output;