Skip to content

Instantly share code, notes, and snippets.

@kas-cor
Last active November 11, 2019 08:50
Show Gist options
  • Save kas-cor/4b4063088104454d2bf47766751fb278 to your computer and use it in GitHub Desktop.
Save kas-cor/4b4063088104454d2bf47766751fb278 to your computer and use it in GitHub Desktop.
Component API sms.ru for Yii2
<?php
namespace app\components;
use yii\base\Component;
use yii\httpclient\Client;
use yii\httpclient\Exception;
/**
* Class Smsru
* @package app\components
* @author kas-cor <[email protected]>
* @github https://gist.github.com/kas-cor/4b4063088104454d2bf47766751fb278
* @link http://github.com/kas-cor repositories
*/
class Smsru extends Component {
/**
* Access id API
* @var string
*/
public $api_id;
/**
* @var Client HTTP Client
*/
private $client;
/**
* @inheritDoc
*/
public function init() {
parent::init();
$this->client = new Client([
'baseUrl' => 'http://sms.ru/',
]);
}
/**
* Sending SMS message
* @param string $to
* @param string $message
* @return array
* @throws Exception
*/
public function send($to, $message) {
return $this->post("sms/send", [
"to" => $to,
"text" => $this->convertToUtf8($message),
]);
}
/**
* Getting status SMS
* @param integer $id
* @return array
* @throws Exception
*/
public function status($id) {
return $this->post("sms/status", [
"id" => $id,
]);
}
/**
* Getting cost message
* @param string $to
* @param string $message
* @return array
* @throws Exception
*/
public function cost($to, $message) {
return $this->post("sms/cost", [
"to" => $to,
"text" => $this->convertToUtf8($message),
]);
}
/**
* Getting account balance
* @return array
* @throws Exception
*/
public function balance() {
return $this->post("my/balance");
}
/**
* Getting limits to sending
* @return array
* @throws Exception
*/
public function limit() {
return $this->post("my/limit");
}
/**
* Getting senders
* @return array
* @throws Exception
*/
public function senders() {
return $this->post("my/senders");
}
/**
* Post data
* @param string $method
* @param array $data
* @return array
* @throws Exception
*/
private function post($method, $data = []) {
$response = $this->client->post($method, array_merge(["api_id" => $this->api_id], $data))->send();
if ($response->isOk) {
return explode("\n", $response->content);
}
return false;
}
/**
* Convert text to utf-8
* @param string $text
* @return bool|false|string
*/
private function convertToUtf8($text) {
return !preg_match("//u", $text) ? iconv("windows-1251", "utf-8", $text) : $text;
}
}
@kas-cor
Copy link
Author

kas-cor commented Feb 3, 2017

Installing

Move Smsru.php to folder components in root directiry.

Add to config file:

'components' => [
    'smsru' => [
        'class' => 'app\components\Smsru',
        'api_id' => '{your_api_id}'
    ]
],

Useing

Send SMS

$to = "79031234567";
$message = "Test\nTest\nTest\nTest";
var_dump(Yii::$app->smsru->send($to, $message));

Status SMS

$id = "123456-123456";
var_dump(Yii::$app->smsru->status($id));

Cost SMS

$to = "79031234567";
$message = "Test\nTest\nTest\nTest";
var_dump(Yii::$app->smsru->cost($to, $message));

Accaunt balance

var_dump(Yii::$app->smsru->balance());

Accaunt limits

var_dump(Yii::$app->smsru->limit());

Accaunt senders

var_dump(Yii::$app->smsru->senders());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment