PHP: Codeigniter Library for Eztexting.com API
Just passing on a library I wrote. It may have bugs. Use at your own risk.
Features:
- Automatically split up message into separate messages if max char limit exceeded.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /*** brett: Wed Jul 15 16:34:54 GMT 2009 * A library for interacting with Eztexting API ***/ class Eztexting { public $phonenumber; public $user; public $password; public $subject; private $p_express =1; public $host; private $ci; const express_limit = 160; const standard_limit = 130; const api_code_sent = 1; // Message sent const api_code_invalid_account = -1; // Invalid user or password (make sure your account is API allowed) const api_code_no_credit = -2; // Credit Limit Reached const api_code_opt_out = -5; // Local Opt Out (the recipient/number is on your opt-out list.) const api_code_invalid_msg = -7; // Invalid Message (message exceeds 130 characters (160 for express) or contains single or double quotes) const api_code_unknown = -10; // Unknown Error (please contact our support dept.) const api_express = 1; const api_standard = 0; const api_host = 'https://www.eztexting.com/apisendmessage.php'; function __construct() { $this->ci =& get_instance(); $this->user = 'username'; $this->password = 'password'; $this->host = self::api_host; $this->express = self::api_express; } function sendtext($message, $phonenumber='', $subject='', $express=1) { //change class properties if paramters sent $this->phonenumber = ( empty($phonenumber) ) ? $this->phonenumber : $phonenumber; $this->subject = ( empty($subject) ) ? $this->subject : $subject; $this->express = $express; // determine char limit $limit = $this->get_limit(); //determine who many individual messages need to be sent. if ( strlen($message) > $limit ) { //break message into individual messages. $lines = explode("\n", $message); $line = ''; $total_lines = count($lines); //username and subject appear to be part of 130/160 max chars. $msg_len = ( empty($this->subject) ) ? strlen($this->user . ':') : strlen($this->user . ":\nsubject: " . $this->subject); $messages = array(); $temp_msgs = array(); $last_result = self::api_code_unknown; for($i=0;$i<=$total_lines;$i++) { if ( strlen($lines[$i] . "\n") + $msg_len <= $limit ) { $line = $lines[$i] . "\n"; $temp_msgs[] = $line; $msg_len += strlen($line); } else { $messages[] = implode('', $temp_msgs); $line = "(cont.)\n" . $lines[$i] . "\n"; $temp_msgs = array($line); $msg_len = ( empty($this->subject) ) ? strlen($this->user . ':'. $line) : strlen($this->user . ":\nsubject: " . $this->subject . $line); } } $messages[] = implode('', $temp_msgs); foreach ($messages as $message) { $last_result = $this->send($message); if ( $last_result != self::api_code_sent ) { return $last_result; break; } } return $last_result; } else { return $this->send($message); } } private function send($message){ $message = substr($message, 0, $this->get_limit()); //ensure we do not get an error for having tooo many chars. $post_array = array(); $post_array[] = 'user=' . urlencode($this->user); $post_array[] = 'pass=' . urlencode($this->password); $post_array[] = 'phonenumber=' . urlencode($this->phonenumber); $post_array[] = 'subject=' . urlencode($this->subject); $post_array[] = 'message=' . urlencode($message); $post_array[] = 'express=' . urlencode($this->p_express); $post_string = implode('&',$post_array); $ch=curl_init($this->host); curl_setopt($ch,CURLOPT_POST,1); curl_setopt($ch,CURLOPT_POSTFIELDS,$post_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); $reponse = curl_exec($ch); curl_close($ch); return ( is_numeric($reponse) ) ? intval($reponse) : self::api_code_unknown; } private function get_limit() { return ( $this->p_express == self::api_express ) ? self::express_limit : self::standard_limit; } // Wish php had accessors protected function __set($key, $value) { if ($key == 'express') { $newvalue; if ( ctype_digit($value) ) { $newvalue = $value; } elseif ( is_numeric($value) ) { $newvalue = intval($value); }; if ( $newvalue == 0 || $newvalue == 1 ) { $this->p_express = $newvalue; } } } } ?> |