send( '79087964781', 'Привет, разработчик!');
Example 2 (replace sender + send):
include('smspilot.class.php');
$sms = new SMSPilot( 'XXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZXXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZ', 'WINDOWS-1251' );
if ($sms->send('79121234512','Завтра контрольная!', 'SCHOOL_N2')) {
echo 'Сообщение успешно отправлено
';
echo 'Цена='.$sms->cost.' кредитов
';
echo 'Баланс='.$sms->balance.' кредитов
';
} else
echo 'Ошибка! '.$sms->error;
Example 3 (send + get sms status):
include('smspilot.class.php');
$sms = new SMSPilot( 'XXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZXXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZ' );
$sms->send('79087964781', 'Novy zakaz http://www.smspilot.ru!');
print_r( $sms->status[0] ); // Array ( [id] => 94 [phone] => 79087964781 [zone] => 2 [status] => 0 )
print_r( $sms->statusByPhone( '79087964781' ) ); // // Array ( [id] => 94 [phone] => 79087964781 [zone] => 2 [status] => 0 )
Example 4: (т.к. 79.... запрещён в отправителе, то используем, 89..., указываем также что наша кодировка Windows-1251, проверяем автозамену 89.. получателя)
include('smsplot.class.php');
$sms = new SMSPilot('XXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZXXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZ','WINDOWS-1251');
$status = $sms->send('89087964781', 'Сообщение в Windows-1251 кодировке!', '89088038965');
print_r( $status )
Array
(
[0] => Array
(
[id] => 129
[phone] => 79087964781
[zone] => 2
[status] => 0
)
)
Example 5 (check status):
include('smspilot.class.php');
$sms = new SMSPilot( 'XXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZXXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZ' );
$status = $sms->check( array( 94,95) );
print_r( $status );
Array
(
[0] => Array
(
[id] => 94
[phone] => 79087964781
[zone] => 2
[status] => 2
)
[1] => Array
(
[id] => 95
[phone] => 79087964781
[zone] => 2
[status] => -1
)
)
Example 6 ( balance ):
include('smspilot.class.php');
$sms = new SMSPilot( 'XXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZXXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZ' );
echo $sms->balance(); // 23004
*/
class SMSPilot {
public $api = 'http://smspilot.ru/api.php';
public $apikey = 'XXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZXXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZ';
public $charset = 'UTF-8';
// public $use_ssl = false; //removed in 1.8.3, see $sms->api property
public $to;
public $text;
public $from = false;
public $error;
public $success;
public $status; // new in 1.7
public $info;
public $cost; // new in 1.8
public $balance; // new in 1.8
// public $limit; // removed 1.8
public function __construct( $apikey = false, $charset = false, $from = false ) {
if ($apikey)
$this->apikey = $apikey;
else if (defined('SMSPILOT_APIKEY'))
$this->apikey = SMSPILOT_APIKEY;
if ($charset)
$this->charset = $charset;
else if (defined('SMSPILOT_CHARSET'))
$this->charset = SMSPILOT_CHARSET;
if ($from)
$this->from = $from;
else if (defined('SMSPILOT_FROM'))
$this->from = SMSPILOT_FROM;
if (defined('SMSPILOT_API'))
$this->api = SMSPILOT_API;
}
// send sms via smspilot.ru
public function send( $to = false, $text = false, $from = false ) {
if ($to)
$this->to = $to;
if ( $text )
$this->text = $text;
if ( $from )
$this->from = $from;
$this->error = false;
$this->success = false;
$this->status = array();
$text = ($this->charset != 'UTF-8') ? mb_convert_encoding($this->text, 'utf-8', $this->charset ) : $this->text;
$result = $this->http_post($this->api, array(
'send' => $text,
'to' => ((is_array($this->to)) ? implode(',', $this->to) : $this->to),
'from' => $this->from,
'apikey' => $this->apikey
));
if ($result) {
if (substr($result,0,6) == 'ERROR=') {
$this->error = substr($result,6);
return false;
} elseif (substr($result,0,8) == 'SUCCESS=') {
$this->success = substr($result,8,($p = strpos($result,"\n"))-8);
if (preg_match('~(\d+)/(\d+)~', $this->success, $matches )) {
$this->cost = $matches[1]; // new in 1.8
$this->balance = $matches[2]; // new in 1.8
}
$status_csv = substr( $result, $p+1 );
//status
$status_csv = explode( "\n", $status_csv );
foreach( $status_csv as $line ) {
$s = explode(',', $line);
$this->status[] = array(
'id' => $s[0],
'phone' => $s[1],
'zone' => $s[2],
'status' => $s[3]
);
}
return $this->status;
} else {
$this->error = 'UNKNOWN RESPONSE';
return false;
}
} else {
$this->error = 'CONNECTION ERROR';
return false;
}
}
// check status by sms id or ids
public function check( $ids ) { // new in 1.7
if (is_array($ids))
$ids = implode(',', $ids);
$this->error = false;
$this->success = false;
$this->status = array();
$result = $this->http_post($this->api, array(
'check' => $ids,
'apikey' => $this->apikey
));
if ($result) {
if (substr($result,0,6) == 'ERROR=') {
$this->error = substr($result,6);
return false;
} else {
$status_csv = $result;
//status
$status_csv = explode( "\n", $status_csv );
foreach( $status_csv as $line ) {
$s = explode(',', $line);
$this->status[] = array(
'id' => $s[0],
'phone' => $s[1],
'zone' => $s[2],
'status' => $s[3]
);
}
return $this->status;
}
} else {
$this->error = 'CONNECTION ERROR';
return false;
}
}
// helper to find status by phone
public function statusByPhone( $phone ) {
foreach( $this->status as $s )
if ( $s['phone'] == $phone )
return $s;
return false;
}
public function balance( $currency = 'sms' ) {
$result = $this->http_post($this->api, array(
'balance' => $currency,
'apikey' => $this->apikey
));
if (strlen($result)) {
if (substr($result,0,6) == 'ERROR=') {
$this->error = substr($result, 6);
return false;
} else
return $this->balance = $result;
} else {
$this->error = 'CONNECTION ERROR';
return false;
}
}
// apikey info
public function info() {
$result = $this->http_post( $this->api, array(
'apikey' => $this->apikey
));
if ($result) {
if (substr($result,0,6) == 'ERROR=') {
$this->error = substr($result, 6);
return false;
} elseif (substr($result,0,8) == 'SUCCESS=') {
$s = substr($result,8, ($p = strpos($result,"\n"))-8);
$this->success = $s;
$lines = explode("\n",substr($result,$p));
$this->info = array();
foreach( $lines as $line )
if ($p = strpos($line,'='))
$this->info[ substr($line,0,$p) ] = substr($line,$p+1);
if ($this->charset != 'UTF-8')
foreach( $this->info as $k => $v)
$this->info[ $k ] = mb_convert_encoding($v,$this->charset,'UTF-8');
if (isset($this->info['balance'])) $this->balance = $this->info['balance'];
return true;
} else {
$this->error = 'UNKNOWN RESPONSE';
return false;
}
} else {
$this->error = 'CONNECTION ERROR';
return false;
}
}
// sockets version HTTP/POST
public function http_post( $url, $data ) {
$eol = "\r\n";
$post = '';
if (is_array($data)) {
foreach( $data as $k => $v)
$post .= $k.'='.urlencode($v).'&';
$post = substr($post,0,-1);
$content_type = 'application/x-www-form-urlencoded';
} else {
$post = $data;
if (strpos($post, '