Last active
November 11, 2019 08:50
-
-
Save kas-cor/4b4063088104454d2bf47766751fb278 to your computer and use it in GitHub Desktop.
Component API sms.ru for Yii2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Installing
Move Smsru.php to folder
componentsin root directiry.Add to config file:
Useing
Send SMS
Status SMS
Cost SMS
Accaunt balance
Accaunt limits
Accaunt senders