Il est important de récupérer directement dans votre CRM les contacts qui s'intéresse à votre marque. Pour ce faire, nous disposons d'un script PHP à insérer en API directement lié à votre formulaire web.
<?php
// use your own logic to populate this array
$submittedData=[
"first_name"=>"Jhon",
"last_name"=>"Doe",
"email"=>"jhon@doe.com",
];
$baseUrl = 'https:/pipeline.webmecanik.com';
// to manage safely into you system
$apiKey = '';
$clientSecret = '';
$params = [
'grant_type' => 'client_credentials',
'client_id' => $apiKey,
'client_secret' => $clientSecret,
];
// get Bearer token
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "{$baseUrl}/oauth/token");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$accessTokenResponse = curl_exec($ch);
if(curl_errno($ch)){
throw new Exception(curl_error($ch));
}
curl_close($ch);
$tokenData = json_decode($accessTokenResponse, true);
$accessToken = $tokenData['access_token'] ?? null;
if (!$accessToken) {
throw new Exception('Impossible d\'obtenir le token d\'accès');
}
// check if contact exist in database
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "{$baseUrl}/api/v1/contact?filters[email]={$submittedData['email']}");
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $accessToken,
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$getContactResponse = curl_exec($ch);
if(curl_errno($ch)){
throw new Exception(curl_error($ch));
}
curl_close($ch);
$contact = json_decode($getContactResponse, true);
$contactSlug = $contact['data'][0]['slug'] ?? null;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $accessToken
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (is_null($contactSlug)){
//create contact
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $accessToken
]);
curl_setopt($ch, CURLOPT_URL, "{$baseUrl}/api/v1/contact");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($submittedData));
}
else{
//update contact
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $accessToken,
'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_URL, "{$baseUrl}/api/v1/contact/{$contactSlug}");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($submittedData));
}
$contactResponse = curl_exec($ch);
if(curl_errno($ch)){
throw new Exception(curl_error($ch));
}
curl_close($ch);
Commentaires
0 commentaire
Vous devez vous connecter pour laisser un commentaire.