Last active
August 18, 2016 12:03
-
-
Save kas-cor/d41760321023d2812dd4c5742d635819 to your computer and use it in GitHub Desktop.
Sms.ru send SMS
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 | |
| /** | |
| * @author kas-cor <[email protected]> | |
| * @link http://github.com/kas-cor repositories | |
| */ | |
| namespace cls; | |
| class Smsru { | |
| /** | |
| * Access id API | |
| * @var string | |
| */ | |
| private $api_id; | |
| /** | |
| * @param string $api_id Access id API | |
| */ | |
| public function __construct($api_id) { | |
| $this->api_id = $api_id; | |
| } | |
| /** | |
| * Sending SMS message | |
| * @param string $to | |
| * @param string $message | |
| * @return array | |
| */ | |
| public function send($to, $message) { | |
| $data = array( | |
| "to" => $to, | |
| "text" => $this->convertToUtf8($message) | |
| ); | |
| return $this->post("sms/send", $data); | |
| } | |
| /** | |
| * Getting status SMS | |
| * @param integer $id | |
| * @return array | |
| */ | |
| public function status($id) { | |
| $data = array( | |
| "id" => $id | |
| ); | |
| return $this->post("sms/status", $data); | |
| } | |
| /** | |
| * Getting cost message | |
| * @param string $to | |
| * @param string $message | |
| * @return array | |
| */ | |
| public function cost($to, $message) { | |
| $data = array( | |
| "to" => $to, | |
| "text" => $this->convertToUtf8($message) | |
| ); | |
| return $this->post("sms/cost", $data); | |
| } | |
| /** | |
| * Getting accaunt balance | |
| * @return array | |
| */ | |
| public function balance() { | |
| return $this->post("my/balance"); | |
| } | |
| /** | |
| * Getting limits to sending | |
| * @return array | |
| */ | |
| public function limit() { | |
| return $this->post("my/limit"); | |
| } | |
| /** | |
| * Getting senders | |
| * @return array | |
| */ | |
| public function senders() { | |
| return $this->post("my/senders"); | |
| } | |
| /** | |
| * Post data | |
| * @param string $method | |
| * @return array | |
| */ | |
| private function post($method, $data = array()) { | |
| $ch = curl_init("http://sms.ru/" . $method); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
| curl_setopt($ch, CURLOPT_TIMEOUT, 30); | |
| curl_setopt($ch, CURLOPT_POSTFIELDS, array_merge(array("api_id" => $this->api_id), $data)); | |
| $res = curl_exec($ch); | |
| curl_close($ch); | |
| return explode("\n", $res); | |
| } | |
| /** | |
| * Convert text to utf-8 | |
| * @param string $text | |
| */ | |
| private function convertToUtf8($text) { | |
| if (!preg_match("//u", $text)) { | |
| return iconv("windows-1251", "utf-8", $text); | |
| } else { | |
| return $text; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Useing
Init
Send SMS
Status SMS
Cost SMS
Accaunt balance
Accaunt limits
Accaunt senders