Overview
  • Namespace
  • Class

Namespaces

  • DevStrefa
    • Esemeser

Classes

  • DevStrefa\Esemeser\Esemeser
  • DevStrefa\Esemeser\Message
  • DevStrefa\Esemeser\MessageType
  1 <?php
  2 namespace DevStrefa\Esemeser;
  3 
  4 /**
  5  * Library main class
  6  *
  7  * Esemeser is a simple library for sending Mobile Text messages through esemeser.pl official API
  8  *
  9  * @license https://opensource.org/licenses/MIT MIT
 10  * @author Cichy <d3ut3r@gmail.com>
 11  * @version 1.1.0
 12  *
 13  */
 14 
 15 class Esemeser
 16 {
 17     /**
 18      * @var string Sending messages API endpoint
 19      */
 20     const SEND_URL='https://esemeser.pl/0api/wyslij.php';
 21 
 22     /**
 23      * @var string Checking balance API endpoint
 24      */
 25     const CHECK_URL='https://esemeser.pl/0api/sprawdz.php';
 26 
 27 
 28     private $account;
 29     private $login;
 30     private $password;
 31     private $requestMethod;
 32 
 33 
 34     /**
 35      * Class Constructor
 36      *
 37      * @param string $account Account name
 38      * @param string $login Account login
 39      * @param string $password Account password
 40      */
 41     public function __construct($account=null,$login=null,$password=null)
 42     {
 43          if ($account != null){
 44              $this->setAccount($account);
 45          }
 46 
 47          if ($login!=null){
 48              $this->setLogin($login);
 49          }
 50 
 51          if ($password!=null){
 52              $this->setPassword($password);
 53          }
 54 
 55          //default request method is set to file_get_contents
 56          $this->requestMethod='fgc';
 57 
 58     }
 59 
 60     /**
 61      * Set mechanism used to make requests to API (Allowed values: fgc OR curl) file_get_contents is default
 62      *
 63      * @param $requestMethod
 64      * @throws \InvalidArgumentException *Exception is thrown when invalud request method is provided as argument*
 65      */
 66     public function setRequestMethod($requestMethod){
 67         if ($requestMethod != 'fgc' && $requestMethod !='curl'){
 68             throw new \InvalidArgumentException('Invalid Request Method');
 69         }
 70     }
 71 
 72     /**
 73      * @param string $url
 74      * @return mixed | string
 75      */
 76     private function makeRequest($url)
 77     {
 78         if ($this->requestMethod == 'fgc'){
 79 
 80             return file_get_contents($url);
 81 
 82         } elseif ($this->requestMethod == 'curl'){
 83 
 84             $curl = curl_init();
 85             curl_setopt_array($curl, array(
 86                 CURLOPT_RETURNTRANSFER => 1,
 87                 CURLOPT_URL => $url
 88             ));
 89 
 90             return curl_exec($curl);
 91         }
 92     }
 93 
 94     /**
 95      * Method returns number of possible to send messages of given type
 96      *
 97      * @param string $messageType Type of message
 98      * @return int Number of messages possible to send
 99      * @throws \InvalidArgumentException *Exception is thrown when provided type is incorrect*
100      * @throws \Exception *Exception is thrown when library wasn't able to get number of messages (ie. credentials was wrong)*
101      */
102     public function checkBalance($messageType=null)
103     {
104         $query=array(
105             'konto'=>$this->getAccount(),
106             'login'=>$this->getLogin(),
107             'haslo'=>$this->getPassword()
108         );
109 
110         if ($messageType!==null){
111             if (MessageType::typeIsValid($messageType)) {
112                 $query['rodzaj'] =$messageType;
113             } else{
114                 throw new \InvalidArgumentException('Incorrect Message Type');
115             }
116         }
117 
118         $queryString=http_build_query($query);
119 
120         $result=$this->makeRequest(self::CHECK_URL.'?'.$queryString);
121 
122         if ($result < 0){
123 
124             switch ($result){
125 
126                 case '-1':
127                     throw new \Exception('Incorrect Account name');
128                     break;
129                 case '-2':
130                     throw new \Exception('Incorrect login or password');
131                     break;
132                 default:
133                     throw new \Exception('Unknown Error');
134                     break;
135 
136             }
137 
138         } else {
139 
140             return $result;
141 
142         }
143 
144     }
145 
146     /**
147      * Method will send given message
148      *
149      * @param Message $message instance of message object
150      * @return bool Function return **true** if message was sent
151      * @throws \Exception *Exception is thrown when message wasn't send*
152      */
153     public function send(Message $message)
154     {
155         $query=array(
156             'konto'=>$this->getAccount(),
157             'login'=>$this->getLogin(),
158             'haslo'=>$this->getPassword(),
159             'nazwa'=>$message->getClientName(),
160             'telefon'=>$message->getPhoneNumber(),
161             'tresc'=>$message->getMessage()
162         );
163 
164         if ($message->getMessageType() !== null){
165             $query['rodzaj']=$message->getMessageType();
166         }
167 
168         $queryString=http_build_query($query);
169 
170         $result=$this->makeRequest(self::SEND_URL.'?'.$queryString);
171 
172         if ($result !='OK'){
173 
174             switch ($result){
175 
176                 case '-1':
177                     throw new \Exception('Incorrect Account name');
178                     break;
179                 case '-2':
180                     throw new \Exception('Incorrect login or password');
181                     break;
182                 case '-3':
183                     throw new \Exception('Incorrect phone number');
184                     break;
185                 case 'NIE':
186                     throw new \Exception('Message wasn\'t sent');
187                     break;
188                 default:
189                     throw new \Exception('Unknown Error');
190                     break;
191 
192             }
193 
194         } else {
195 
196             return true;
197 
198         }
199 
200     }
201 
202     /**
203      * Method returns account name
204      * @return string
205      */
206     public function getAccount()
207     {
208         return $this->account;
209     }
210 
211     /**
212      * Method for setting account name
213      * @param string $account Account name
214      * @return Esemeser
215      */
216     public function setAccount($account)
217     {
218         $this->account = $account;
219         return $this;
220     }
221 
222     /**
223      * Method returns account login
224      * @return string
225      */
226     public function getLogin()
227     {
228         return $this->login;
229     }
230 
231     /**
232      * Method for setting login to account
233      * @param string $login
234      * @return Esemeser
235      */
236     public function setLogin($login)
237     {
238         $this->login = $login;
239         return $this;
240     }
241 
242     /**
243      * Method return password
244      * @return string
245      */
246     public function getPassword()
247     {
248         return $this->password;
249     }
250 
251     /**
252      * Method for setting password to account
253      * @param string $password
254      * @return Esemeser
255      */
256     public function setPassword($password)
257     {
258         $this->password = $password;
259         return $this;
260     }
261 
262 
263 
264 
265 
266 }
API documentation generated by ApiGen