📜 ⬆️ ⬇️

We send SMS.ru with convenience

There was a case, skipped on the article about the sms.ru service
I tried, it works. Liked. Then this was the case.
But recently it took me to send SMS to subscribing customers. The API on the site of the service is well documented and examples are given, but lack of usability. As a result, I wrote a small class for interacting with sms.ru.



<?php

/**
* sms.ru
*/
class Z_Service_Sms {

protected $_apiId = NULL;
protected $_responseCode = NULL;
protected $_lastAction = NULL;

const HOST = 'http://sms.ru/' ;
const SEND = 'sms/send?' ;
const STATUS = 'sms/status?' ;
const BALANCE = 'my/balance?' ;
const LIMIT = 'my/limit?' ;

protected $_responseCodeTranstale = array(
'send' => array(
'100' => ' ' ,
'200' => ' api_id' ,
'201' => ' ' ,
'202' => ' ' ,
'203' => ' ' ,
'204' => ' ' ,
'205' => ' ( 5 )' ,
'206' => ' ' ,
'207' => ' ' ,
'208' => ' time ' ,
'210' => ' GET, POST' ,
'211' => ' ' ,
'220' => ' , .' ,
),
'status' => array(
'-1' => ' ' ,
'100' => ' ' ,
'101' => ' ' ,
'102' => ' ( )' ,
'103' => ' ' ,
'104' => ' : ' ,
'105' => ' : ' ,
'106' => ' : ' ,
'107' => ' : ' ,
'108' => ' : ' ,
'200' => ' api_id' ,
'210' => ' GET, POST' ,
'211' => ' ' ,
'220' => ' , ' ,
),
'balance' => array(
'100' => ' ' ,
'200' => ' api_id' ,
'210' => ' GET, POST' ,
'211' => ' ' ,
'220' => ' , .' ,
),
'limit' => array(
'100' => ' ' ,
'200' => ' api_id' ,
'210' => ' GET, POST' ,
'211' => ' ' ,
'220' => ' , .' ,
),
);

/**
*
* api_id
* @param string $id
*/
public function __construct($id)
{
$ this ->_apiId = $id;
}

/**
*
* @param string $to : 11 . 79060000000
* @param string $text
* @param string $from
* @return string id
*/
public function send($to,$text,$ from =NULL)
{
$apiParams[ 'api_id' ] = $ this ->_apiId;
$apiParams[ 'to' ] = $to;
$apiParams[ 'text' ] = $text;
if ($ from )
$apiParams[ 'from' ] = $ from ;
$url = self::HOST.self::SEND.http_build_query($apiParams);;
$body = file_get_contents($url);
@list($code,$smsId) = explode( "\n" , $body);
$ this ->_lastAction = 'send' ;
$ this ->_responseCode = $code;
return $smsId;
}

/**
*
* @param string $id id
* @return string .
*/
public function status($id)
{
$apiParams[ 'api_id' ] = $ this ->_apiId;
$apiParams[ 'id' ] = $id;
$url = self::HOST.self::STATUS.http_build_query($apiParams);
$body = file_get_contents($url);
$status = $body;
$ this ->_lastAction = 'status' ;
$ this ->_responseCode = $status;
return $status;
}

/**
*
* @return string
*/
public function balance()
{
$apiParams[ 'api_id' ] = $ this ->_apiId;
$url = self::HOST.self::BALANCE.http_build_query($apiParams);
$body = file_get_contents($url);
@list($code,$balance) = explode( "\n" , $body);
$ this ->_lastAction = 'balance' ;
$ this ->_responseCode = $code;
return $balance;
}

/**
*
* @return int
*/
public function limit()
{
$apiParams[ 'api_id' ] = $ this ->_apiId;
$url = self::HOST.self::LIMIT.http_build_query($apiParams);
$body = file_get_contents($url);
@list($code,$count,$limit) = explode( "\n" , $body);
$ this ->_lastAction = 'limit' ;
$ this ->_responseCode = $code;
return ( int )($count - $limit);
}

/**
*
* @return string
*/
public function getResponseCode()
{
return $ this ->_responseCode;
}

/**
*
* @return string
*/
public function getResponseMessage()
{
if ($ this ->_lastAction)
return $ this ->_responseCodeTranstale[$ this ->_lastAction][$ this ->getResponseCode()];
else
return ' ' ;
}

}

* This source code was highlighted with Source Code Highlighter .

')
Please use and give advice on improvement.

Source: https://habr.com/ru/post/100750/


All Articles