/** * Send SMS */ public static function send() { $recipients = SmsHandler::$model->recipients; $settings = Yii::app()->settings->get(Constants::CATEGORY_SMS, array(Constants::KEY_SMS_AFRICASTALKING_USERNAME, Constants::KEY_SMS_AFRICASTALKING_API_KEY, Constants::KEY_SMS_AFRICASTALKING_SHORTCORD)); if (!is_array($recipients)) { $recipients = explode(',', $recipients); } if (empty(SmsHandler::$model->from)) { SmsHandler::$model->from = $settings[Constants::KEY_SMS_AFRICASTALKING_SHORTCORD]; } $gateway = new AfricaStalkingGateway($settings[Constants::KEY_SMS_AFRICASTALKING_USERNAME], $settings[Constants::KEY_SMS_AFRICASTALKING_API_KEY]); $recipients_count = count($recipients); $sent_count = 0; SmsHandler::$model->batch_id = MsgSmsOutbox::model()->getNextIntegerID('batch_id'); $success_count = 0; $chunks = SmsHandler::getChunks($recipients, self::MAX_CHUNK_LENGTH, $recipients_count); $currency = Yii::app()->settings->get(Constants::CATEGORY_GENERAL, Constants::KEY_CURRENCY, SettingsCurrency::CURRENCY_KES); $org_model = !empty(SmsHandler::$model->org_id) ? Org::model()->loadModel(SmsHandler::$model->org_id) : NULL; $price_per_sms = NULL !== $org_model ? $org_model->price_per_sms : NULL; foreach ($chunks as $contacts) { $error = FALSE; $status_message = NULL; if (SmsHandler::$model->recipients_source === MsgSmsOutbox::RECIPIENTS_SOURCE_DEFAULT) { $gsm_numbers = SmsHandler::preparePhoneNumbers($contacts, FALSE); } else { $gsm_numbers = implode(",", $contacts); } $results = $gateway->sendMessage($gsm_numbers, SmsHandler::$model->message, SmsHandler::$model->from); if (empty($results)) { $status_message = "Oops, No messages were sent. ErrorMessage: " . $gateway->getErrorMessage(); $error = TRUE; } if (!empty(SmsHandler::$queue_id)) { if ($error === FALSE) { $sent_count = $sent_count + count($contacts); $status_message = Lang::t("Processed {$sent_count} of {$recipients_count} messages"); } ConsoleTaskQueue::model()->updateProgress(SmsHandler::$queue_id, $status_message); //update status } else { if ($error === TRUE && NULL !== $status_message) { Yii::log($status_message, CLogger::LEVEL_ERROR); } } if ($error === FALSE) { $success_count += self::saveRecipients(SmsHandler::$model, $results, $currency, $price_per_sms); } sleep(1); } if (NULL !== $org_model) { SmsHandler::addBilling($org_model, SmsHandler::$model, $success_count); } if (!empty(SmsHandler::$queue_id)) { ConsoleTaskQueue::model()->updateProgress(SmsHandler::$queue_id, Lang::t('Completed.'), ConsoleTaskQueue::STATUS_COMPLETED); } }
<?php // Include the helper gateway class require_once 'AfricasTalkingGateway.php'; // Specify your login credentials $username = "******"; $apikey = "b363386c84d13496370e9a4b8ba60d8daa98e4a6a41289d392e08b2ee7996f41"; // Create a new instance of our awesome gateway class $gateway = new AfricaStalkingGateway($username, $apikey); // Any gateway errors will be captured by our custom Exception class below, // so wrap the call in a try-catch block try { // Our gateway will return 100 messages at a time back to you, starting with // what you currently believe is the lastReceivedId. Specify 0 for the first // time you access the gateway, and the ID of the last message we sent you // on subsequent results $lastReceivedId = 0; // Here is a sample of how to fetch all messages using a while loop do { $results = $gateway->fetchMessages($lastReceivedId); foreach ($results as $result) { echo " From: " . $result->from; echo " To: " . $result->to; echo " Message: " . $result->text; echo " Date Sent: " . $result->date; echo " LinkId: " . $result->linkId; echo " id: " . $result->id; echo "\n"; $lastReceivedId = $result->id; } } while (count($results) > 0);
private function send_sms($phone, $message) { require_once 'Gateway.php'; $username = "******"; /*$apiKey = "8ad24c5ad1e03125110a8310bb6c1977ab33d3c72fd6dd7c064335f7fa064456";*/ $apiKey = "508c63f78ed69654e83d756c9f19a944ee96c4d0f35bc272e74576f54994daba"; $recipients = $this->cleanPhone($phone); $gateway = new AfricaStalkingGateway($username, $apiKey); try { // Thats it, hit send and we'll take care of the rest. $results = $gateway->sendMessage($recipients, $message); $data = array('to' => $recipients, 'message' => $message, 'datetime' => date('Y-m-d H:i:s')); $this->Outbox->save($data); } catch (AfricasTalkingGatewayException $e) { return false; } }