static function get_messages_xml($messages) { ob_start(); echo "<?xml version='1.0' encoding='UTF-8'?>\n"; echo "<response>"; echo "<messages>"; foreach ($messages as $message) { $type = isset($message->type) ? $message->type : EnvayaSMS::MESSAGE_TYPE_SMS; $id = isset($message->id) ? " id=\"" . EnvayaSMS::escape($message->id) . "\"" : ""; $to = isset($message->to) ? " to=\"" . EnvayaSMS::escape($message->to) . "\"" : ""; $priority = isset($message->priority) ? " priority=\"" . $message->priority . "\"" : ""; echo "<{$type}{$id}{$to}{$priority}>" . EnvayaSMS::escape($message->message) . "</{$type}>"; } echo "</messages>"; echo "</response>"; return ob_get_clean(); }
<?php /* * This example script implements the EnvayaSMS API. * * It sends an auto-reply to each incoming message, and sends outgoing SMS * that were previously queued by example/send_sms.php . * * To use this file, set the URL to this file as as the the Server URL in the EnvayaSMS app. * The password in the EnvayaSMS app settings must be the same as $PASSWORD in config.php. */ require_once dirname(__DIR__) . "/config.php"; require_once dirname(dirname(__DIR__)) . "/EnvayaSMS.php"; $request = EnvayaSMS::get_request(); header("Content-Type: {$request->get_response_type()}"); if (!$request->is_validated($PASSWORD)) { header("HTTP/1.1 403 Forbidden"); error_log("Invalid password"); echo $request->render_error_response("Invalid password"); return; } $action = $request->get_action(); switch ($action->type) { case EnvayaSMS::ACTION_INCOMING: // Send an auto-reply for each incoming message. $type = strtoupper($action->message_type); error_log("Received {$type} from {$action->from}"); error_log(" message: {$action->message}"); if ($action->message_type == EnvayaSMS::MESSAGE_TYPE_MMS) { foreach ($action->mms_parts as $mms_part) { $ext_map = array('image/jpeg' => 'jpg', 'image/gif' => 'gif', 'text/plain' => 'txt', 'application/smil' => 'smil');
function render_error_response($message) { if ($this->supports_json()) { return json_encode(array('error' => array('message' => $message))); } else { ob_start(); echo "<?xml version='1.0' encoding='UTF-8'?>\n"; echo "<response>"; echo "<error>"; echo EnvayaSMS::escape($message); echo "</error>"; echo "</response>"; return ob_get_clean(); } }
$sms->from = $data['from']; $sms->message = $data['message']; $messages[] = $sms; } } } closedir($dir); echo $action->get_response_xml($messages); return; case EnvayaSMS::ACTION_SEND_STATUS: $id = $action->id; // delete file with matching id if (preg_match('#^\\w+$#', $id) && unlink("{$OUTGOING_DIR_NAME}/{$id}.json")) { echo EnvayaSMS::get_success_xml(); } else { header("HTTP/1.1 404 Not Found"); echo EnvayaSMS::get_error_xml("Invalid id"); } return; case EnvayaSMS::ACTION_DEVICE_STATUS: error_log("device_status = {$action->status}"); echo EnvayaSMS::get_success_xml(); return; case EnvayaSMS::ACTION_TEST: echo EnvayaSMS::get_success_xml(); return; default: header("HTTP/1.1 404 Not Found"); echo EnvayaSMS::get_error_xml("Invalid action"); return; }