Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Esemeser\Api\Simple;
- /**
- * Class to handle the api provided by esemeser.pl
- *
- * This class based on document: http://esemeser.pl/0pdf/api-dokumentacja.pdf and use CURL to make request.
- * Another way to do this is using Esemeser\Api\Soap\Sender class which using SOAP to communicate with API.
- *
- * @category Esemeser
- * @package API
- * @subpackage Simple
- * @version 0.9
- * @since 10.11.2012
- * @author d3ut3r
- */
- class Sender {
- /**
- * Account name on esemeser.pl
- * @var string
- */
- private $_account;
- /**
- * Account login
- * @var string
- */
- private $_login;
- /**
- * Account password
- * @var string
- */
- private $_password;
- /**
- * Type of message.
- * This variable can contain one of $_allowedTypes values
- * @var string
- */
- private $_type;
- /**
- * Recipient name (max 80 signs length)
- * @var string
- */
- private $_recipientName;
- /**
- * Recipient phone number (exactly nine digits)
- * @var string
- */
- private $_recipientNumber;
- /**
- * Message body (max 160 signs).
- * @var string
- */
- private $_message;
- /**
- * Array of allowed message types.
- * @var array
- */
- private $_allowedTypes = array(
- 'standard',
- 'plus'
- );
- /**
- * Checks if the method curl_init exists and if not it throw exception.
- * @throws \BadFunctionCallException
- */
- public function __construct() {
- if (!function_exists('curl_init')) {
- throw new \BadFunctionCallException('Can\'t find CURL library');
- }
- }
- /**
- * Setting for sender
- * @param string
- * @param string
- * @param string
- * @throws \InvalidArgumentException
- */
- public function setSender($accountName, $login, $password) {
- if (empty($accountName) || empty($login) || empty($password)) {
- throw new \InvalidArgumentException('You must specify account name and password');
- }
- $this->_account = $accountName;
- $this->_login = $login;
- $this->_password = $password;
- }
- /**
- * Set type of message
- * @param string
- * @throws \InvalidArgumentException
- */
- public function setType($type) {
- if (!in_array($type, $this->_allowedTypes)) {
- throw new \InvalidArgumentException('Unsupported message type');
- }
- $this->_type = $type;
- }
- /**
- * Set number and name of recipient
- * @param string
- * @param number
- * @throws \InvalidArgumentException
- */
- public function setRecipient($recipientName, $recipientNumber) {
- if (!preg_match('/^[0-9]{9}$/', $recipientNumber)) {
- throw new \InvalidArgumentException('Incorrect number');
- }
- if (mb_strlen($recipientName) > 80) {
- throw new \InvalidArgumentException('Recipient name is too long');
- }
- $this->_recipientName = $recipientName;
- $this->_recipientNumber = $recipientNumber;
- }
- /**
- * Set message content, this method also replace prohibited chars like ' " /
- * if second parameter $autoTrim is true, the message will be truncated to 160 characters automatically otherwise method throws Exception if message will be longer than 160 signs
- *
- * @param string $message
- * @param boolean $autoTrim
- * @throws \InvalidArgumentException
- */
- public function setMessage($message, $autoTrim = false) {
- $prohibitedChars = array(
- '\'',
- '"',
- '/'
- );
- $replacement = array(
- ' ',
- ' ',
- '\\'
- );
- if (mb_strlen($message) > 160 && $autoTrim == false) {
- throw new \InvalidArgumentException('Message is too long');
- } else if (mb_strlen($message) > 160 && $autoTrim == true) {
- $message = mb_substr($message, 0, 160);
- }
- $this->_message = str_replace($prohibitedChars, $replacement, $message);
- }
- /**
- * Send message and return true or throw exception
- * @return boolean
- * @throws \BadMethodCallException
- * @throws \InvalidArgumentException
- * @throws \RuntimeException
- */
- public function send() {
- //TODO: Add choice if we want using https or http
- if (empty($this->_account) || empty($this->_login) || empty($this->_password) || empty($this->_recipientNumber)) {
- throw new \BadMethodCallException('You do it wrong! please read examples files first.');
- }
- $requestVars = array();
- $requestVars['konto'] = $this->_account;
- $requestVars['login'] = $this->_login;
- $requestVars['haslo'] = $this->_password;
- if (!empty($this->_type)) {
- $requestVars['rodzaj'] = $this->_type;
- }
- $requestVars['nazwa'] = $this->_recipientName;
- $requestVars['telefon'] = $this->_recipientNumber;
- $requestVars['tresc'] = $this->_message;
- //init curl and send request
- $ch = curl_init('http://esemeser.pl/0api/wyslij.php');
- curl_setopt($ch, CURLOPT_POST,1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($requestVars));
- curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
- curl_setopt($ch, CURLOPT_FAILONERROR,true);
- $response = curl_exec($ch);
- if ($response === false){
- $errormsg=curl_error($ch);
- curl_close($ch);
- throw new \RuntimeException($errormsg);
- }
- curl_close($ch);
- switch ($response){
- case 'OK':
- return true;
- break;
- case '-1':
- throw new \InvalidArgumentException('Account '.$this->_account.' does not exists');
- break;
- case '-2':
- throw new \InvalidArgumentException('Incorrect login or password');
- break;
- case '-3':
- throw new \InvalidArgumentException('Incorrect recipient number');
- break;
- case 'NIE':
- default:
- throw new \RuntimeException('Message has not been sent from unknown reasons');
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement