Notes
Notes - notes.io |
// ringba_functions.php
/**
* Format phone number for Ringba API by adding +1 prefix
*/
function formatPhoneForRingba($phone) {
// Remove any non-numeric characters
$numericOnly = preg_replace('/D/', '', $phone);
// Add +1 prefix if it's a 10-digit number
if (strlen($numericOnly) === 10) {
return "+1{$numericOnly}";
}
// Otherwise, assume it already has country code
return "+{$numericOnly}";
}
/**
* Fetch data from Ringba API for the given entries
*
* @param array $entries Array of entry data containing phone numbers
* @return array Associative array of Ringba data keyed by formatted phone number
*/
function fetchRingbaData($entries) {
// Ringba API credentials and configuration
$RINGBA_CONFIG = [
'accountId' => 'RA6e79d917a2be48888355663cac09d557', // Replace with your Ringba account ID
'apiToken' => '09f0c9f0e72a39528090b34cb7305c4a91dda58c0a1cf4975b84c7c5f9141c9bd09f325151805f98a5e5be0c371d0e5aa908510ae257a3d8e07096e15c374f5737cd3f15c6f4c19c993a615f512e3ca85ca925305d52fffacac736f54f33b494b380609ebab4640a82a6c33715bab490f0ff3a11' // Replace with your Ringba API token
];
$ringbaData = [];
if (empty($entries)) {
error_log("No entries to process for Ringba");
return $ringbaData;
}
// Prepare phone numbers for Ringba query
$phones = [];
foreach ($entries as $entry) {
if (!empty($entry['phone_number'])) {
$formattedPhone = formatPhoneForRingba($entry['phone_number']);
$phones[$formattedPhone] = $entry['did'];
}
}
if (empty($phones)) {
error_log("No valid phone numbers found for Ringba query");
return $ringbaData;
}
// Build Ringba API query with phone conditions
$phoneConditions = [];
foreach ($phones as $phone => $did) {
$phoneConditions[] = [
'column' => 'inboundPhoneNumber',
'value' => $phone,
'comparisonType' => 'EQUALS'
];
}
$requestData = [
'reportStart' => date('c', strtotime('-30 days')),
'reportEnd' => date('c'),
'filters' => [
[
'anyConditionToMatch' => $phoneConditions
]
]
];
try {
// Make API request to Ringba
$ch = curl_init("https://api.ringba.com/v2/{$RINGBA_CONFIG['accountId']}/calllogs");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Token ' . $RINGBA_CONFIG['apiToken'],
'Content-Type: application/json',
'Accept: application/json'
],
CURLOPT_POSTFIELDS => json_encode($requestData),
CURLOPT_SSL_VERIFYPEER => false
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode !== 200) {
error_log("Ringba API Error: HTTP $httpCode - $response");
return $ringbaData;
}
$data = json_decode($response, true);
if (!empty($data['report']['records'])) {
foreach ($phones as $phone => $did) {
// Find matching calls where DID appears in targetName
$matchingCalls = array_filter($data['report']['records'], function($call) use ($phone, $did) {
// First check if required fields exist
if (!isset($call['inboundPhoneNumber']) || !isset($call['targetName'])) {
error_log("Missing required fields in Ringba response for phone: $phone");
return false;
}
// Now safely perform the matching
$phoneMatch = $call['inboundPhoneNumber'] === $phone;
$targetMatch = !empty($call['targetName']) && stripos($call['targetName'], $did) !== false;
// Log matching attempts for debugging
error_log(sprintf(
"Matching attempt - Phone: %s, DID: %s, Ringba Phone: %s, Target: %s, Match: %s",
$phone,
$did,
$call['inboundPhoneNumber'],
$call['targetName'],
($phoneMatch && $targetMatch) ? 'Yes' : 'No'
));
return $phoneMatch && $targetMatch;
});
if (!empty($matchingCalls)) {
$latestCall = array_shift($matchingCalls);
$ringbaData[$phone] = [
'callLength' => isset($latestCall['connectedCallLengthInSeconds']) ?
formatCallLength($latestCall['connectedCallLengthInSeconds']) : 'N/A',
'callDate' => isset($latestCall['callDt']) ?
date('m/d/Y H:i:s', strtotime($latestCall['callDt'])) : 'N/A',
'targetName' => isset($latestCall['targetName']) ? $latestCall['targetName'] : 'N/A',
'conversionAmount' => isset($latestCall['conversionAmount']) ?
'$' . number_format($latestCall['conversionAmount'], 2) : '$0.00'
];
} else {
// Log when no matches are found
error_log("No matching calls found for phone: $phone with DID: $did");
}
}
}
curl_close($ch);
return $ringbaData;
} catch (Exception $e) {
error_log("Ringba API Exception: " . $e->getMessage());
return $ringbaData;
}
}
/**
* Format call length in seconds to a readable format
*/
function formatCallLength($seconds) {
if (!$seconds) return 'N/A';
if ($seconds < 60) {
return $seconds . 's';
}
$minutes = floor($seconds / 60);
$remainingSeconds = $seconds % 60;
return $minutes . 'm ' . $remainingSeconds . 's';
}
?>
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team
