$from,
'to' => $to,
'text' => $message,
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $api_url.'/v2/accounts/'.$accountSID.'/messages');
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, 1);
// Set user and password
curl_setopt ($curl, CURLOPT_USERPWD, $accountSID.':'.$password);
// Do not check SSL
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
// Add header
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
// Set POST
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($body));
$result = curl_exec($curl);
if ($result) {
$result = json_decode($result, true);
} else {
$result = array(
'error_text' => curl_error($curl),
'error_code' => curl_errno($curl),
'status_code' => 600,
);
}
return $result;
}
/**
* This function checks the current status of the message
* @param string $api_url URL to retrieve Apifonica API
* @param string $accountSID Your Apifonica account identifier
* @param string $password Password for your Apifonica account
* @param string $smsuri SMS URL for check status
* @return array
*/
function checkSMS($api_url, $accountSID, $password, $smsuri) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $api_url.$smsuri);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, 1);
// Set user and password
curl_setopt ($curl, CURLOPT_USERPWD, $accountSID.':'.$password);
// Do not check SSL
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
// Add header
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($curl);
if ($result) {
$result = json_decode($result, true);
} else {
$result = array(
'error_text' => curl_error($curl),
'error_code' => curl_errno($curl),
'status_code' => 600,
);
}
return $result;
}
/**
* Set default variables
*/
// Specify Apifonica API URL
$api_url = 'https://api.apifonica.com';
// Specify your Apifonica account SID
$accountSID = '';
// Specify your Apifonica account password
$password = '';
// Specify the message sender's number (this number must be mobile number belonging to the Apifonica account you have specified)
$from = '';
/**
* Set variables from POST
*/
$action = isset($_POST['action'])?$_POST['action']:'default';
$email = isset($_POST['email'])?trim($_POST['email']):'';
$name = isset($_POST['name'])?trim($_POST['name']):'';
$message = isset($_POST['message'])?trim($_POST['message']):'';
$allsms = isset($_POST['allsms'])?trim($_POST['allsms']):'';
$result = false;
$numbers = "";
/**
* Send Bulk SMS
*/
if ($action == 'sendsms') {
//Remove all unnecessary things from the string
$str = preg_replace('/[^0-9,]/', '', $_POST["numbers"]);
$list = explode(",", $str);
$arr_succesful = array();
$fail = "";
foreach ($list as $key => $val) {
if ($val){
$b = false;
// Check if the phone number is valid
if (preg_match("/^\+?[\d\s]+\(?[\d\s]{10,}$/", $val)) $b = true;
if ($b){
$result = sendSMS($api_url, $accountSID, $password, $from, $val, $message);
if ($result['status_code'] == '201') $arr_succesful[$val] = $result['uri'];
}
else{
$fail .= "" . $val . " Status = Phone number is invalid
";
}
}
}
$out = "";
foreach ($arr_succesful as $key => $val) {
$out .= "" . $key . ", Status = Message is successfully sent.
";
}
//Serialize data for further SMS delivery checks
$allsms = serialize($arr_succesful);
$out .= $fail;
if ($result['status_code'] > 299) {
// Display an error message and the web entry form if the SMS send action is failed
$action = 'view';
}
/**
* Check statuses of SMS notifications sent
*/
} else if ($action == 'checksms') {
if ($allsms){
$sms_status = unserialize($allsms);
$check_text = '';
foreach ($sms_status as $key => $val) {
$result = checkSMS($api_url, $accountSID, $password, $val);
if ($result && isset($result['status'])) {
switch ($result['status']) {
case 'queued':
$check_text .= $key . ', Status = Message is on its way!
';
break;
case 'sent':
$check_text .= $key . ', Status = Message is successfully sent.
';
break;
case 'delivered':
$check_text .= $key . ', Status = Message is delivered to recipient\'s phone.
';
break;
case 'failed':
$check_text .= $key . ', Status = Message delivery failed.
';
break;
default:
}
}
}
}
}
/**
* Default view form (Mass Text Messaging)
*/
?>