Современные решения ИТ задач и программной инженерии

PHP класс для работы с BSG СМС API

24.03.2016 в разделах ИнтернетПрограммированиес тегами phpсмсsmsapi
PHP класс для работы с BSG СМС API
Работая с компанией по СМС рассылке https://bsg.hk/ru/ можно столкнуться с проблемой, что у них нет уже готовых библиотек с примерами по работе с их API. Нет, какой то кусок PHP кода там был на примере отправки СМС сообщения. Но API позволяет делать намного больше и пришлось городить свой класс. Пожалуйста, берите и используйте.

Файл с исходным кодом: BsgSmsSenderXml.php, он же ниже с подстветкой.

<?php
  /*
    Free to use!
    http://wds4.com/
  */

  class BsgSmsSenderXml {
    private $Login = '';
    private $ApiPassword = '';
    private $AlphaName = '';
    private $PhoneNumber = '';
    private $SendText = '';
    private $ApiUrl = '';

    public function SetLogin($value) {
      $this->Login = $value;
      return $this;
    }

    public function SetPassword($value) {
      $this->ApiPassword = $value;
      return $this;
    }

    public function SetAlpha($value) {
      $this->AlphaName = $value;
      return $this;
    }

    public function SetPhone($value) {
      $this->PhoneNumber = $value;
      return $this;
    }

    public function SetText($value) {
      $this->SendText = $value;
      return $this;
    }

    public function SendSms() {
      $this->ApiUrl = 'http://app.bsg.hk/xml';
      $XmlData = '';
      $XmlData .= '<?xml version = "1.0" encoding = "utf-8"?>'."n";
      $XmlData .= '<request>'."n";
      $XmlData .= '  <message type = "sms">'."n";
      $XmlData .= '    <sender>'.$this->AlphaName.'</sender>'."n";
      $XmlData .= '    <text>'.$this->SendText.'</text>'."n";
      $XmlData .= '    <abonent phone="'.$this->PhoneNumber.'" number_sms="1" />'."n";
      $XmlData .= '  </message>'."n";
      $XmlData .= '  <security>'."n";
      $XmlData .= '    <login value="'.$this->Login.'" />'."n";
      $XmlData .= '    <password value="'.$this->ApiPassword.'" />'."n";
      $XmlData .= '  </security>'."n";
      $XmlData .= '</request>';
      $Result = '';
      $CurlObject = curl_init($this->ApiUrl);
      curl_setopt($CurlObject, CURLOPT_URL, $this->ApiUrl);
      curl_setopt($CurlObject, CURLOPT_SSL_VERIFYHOST, false);
      curl_setopt($CurlObject, CURLOPT_POST, true);
      curl_setopt($CurlObject, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
      curl_setopt($CurlObject, CURLOPT_POSTFIELDS, $XmlData);
      curl_setopt($CurlObject, CURLOPT_RETURNTRANSFER, true);
      $Result = curl_exec($CurlObject);
      curl_close($CurlObject);
      $p = xml_parser_create();
      if(xml_parse_into_struct($p, $Result, $vals, $index)) {
        xml_parser_free($p);
        return $vals;
      } else {
        xml_parser_free($p);
        return false;
      }
    }

    public function GetSmsStatus($value) {
      $this->ApiUrl = 'http://app.bsg.hk/xml/stat';
      $XmlData = '';
      $XmlData .= '<?xml version = "1.0" encoding = "utf-8"?>'."n";
      $XmlData .= '<request>'."n";
      $XmlData .= '  <get_state>'."n";
      $XmlData .= '    <id_sms>'.$value.'</id_sms>'."n";
      $XmlData .= '  </get_state>'."n";
      $XmlData .= '  <security>'."n";
      $XmlData .= '    <login value="'.$this->Login.'" />'."n";
      $XmlData .= '    <password value="'.$this->ApiPassword.'" />'."n";
      $XmlData .= '  </security>'."n";
      $XmlData .= '</request>';
      $Result = '';
      $CurlObject = curl_init($this->ApiUrl);
      curl_setopt($CurlObject, CURLOPT_URL, $this->ApiUrl);
      curl_setopt($CurlObject, CURLOPT_SSL_VERIFYHOST, false);
      curl_setopt($CurlObject, CURLOPT_POST, true);
      curl_setopt($CurlObject, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
      curl_setopt($CurlObject, CURLOPT_POSTFIELDS, $XmlData);
      curl_setopt($CurlObject, CURLOPT_RETURNTRANSFER, true);
      $Result = curl_exec($CurlObject);
      curl_close($CurlObject);
      $p = xml_parser_create();
      if(xml_parse_into_struct($p, $Result, $vals, $index)) {
        xml_parser_free($p);
        return $vals;
      } else {
        xml_parser_free($p);
        return false;
      }
    }

    public function GetBalance() {
      $this->ApiUrl = 'http://app.bsg.hk/xml/balance';
      $XmlData = '';
      $XmlData .= '<?xml version = "1.0" encoding = "utf-8"?>'."n";
      $XmlData .= '<request>'."n";
      $XmlData .= '  <security>'."n";
      $XmlData .= '    <login value="'.$this->Login.'" />'."n";
      $XmlData .= '    <password value="'.$this->ApiPassword.'" />'."n";
      $XmlData .= '  </security>'."n";
      $XmlData .= '</request>';
      $Result = '';
      $CurlObject = curl_init($this->ApiUrl);
      curl_setopt($CurlObject, CURLOPT_URL, $this->ApiUrl);
      curl_setopt($CurlObject, CURLOPT_SSL_VERIFYHOST, false);
      curl_setopt($CurlObject, CURLOPT_POST, true);
      curl_setopt($CurlObject, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
      curl_setopt($CurlObject, CURLOPT_POSTFIELDS, $XmlData);
      curl_setopt($CurlObject, CURLOPT_RETURNTRANSFER, true);
      $Result = curl_exec($CurlObject);
      curl_close($CurlObject);
      $p = xml_parser_create();
      if(xml_parse_into_struct($p, $Result, $vals, $index)) {
        xml_parser_free($p);
        return $vals;
      } else {
        xml_parser_free($p);
        return false;
      }
    }

    public function GetPhoneInfo($value) {
      $this->ApiUrl = 'http://app.bsg.hk/xml/def';
      $XmlData = '';
      $XmlData .= '<?xml version = "1.0" encoding = "utf-8"?>'."n";
      $XmlData .= '<request>'."n";
      $XmlData .= '  <phones>'."n";
      $XmlData .= '    <phone>'.$value.'</phone>'."n";
      $XmlData .= '  </phones>'."n";
      $XmlData .= '  <security>'."n";
      $XmlData .= '    <login value="'.$this->Login.'" />'."n";
      $XmlData .= '    <password value="'.$this->ApiPassword.'" />'."n";
      $XmlData .= '  </security>'."n";
      $XmlData .= '</request>';
      $Result = '';
      $CurlObject = curl_init($this->ApiUrl);
      curl_setopt($CurlObject, CURLOPT_URL, $this->ApiUrl);
      curl_setopt($CurlObject, CURLOPT_SSL_VERIFYHOST, false);
      curl_setopt($CurlObject, CURLOPT_POST, true);
      curl_setopt($CurlObject, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
      curl_setopt($CurlObject, CURLOPT_POSTFIELDS, $XmlData);
      curl_setopt($CurlObject, CURLOPT_RETURNTRANSFER, true);
      $Result = curl_exec($CurlObject);
      curl_close($CurlObject);
      $p = xml_parser_create();
      if(xml_parse_into_struct($p, $Result, $vals, $index)) {
        xml_parser_free($p);
        return $vals;
      } else {
        xml_parser_free($p);
        return false;
      }
    }
  }

  // Example code, how to use it
  // Functions "SendSms", "GetSmsStatus", "GetBalance", "GetPhoneInfo" return parsed XML array

  // Create BsgSmsSenderXml object and set API data
  $SmsSender = new BsgSmsSenderXml();
  $SmsSender->
  SetLogin('your_login')->
  SetPassword('your_api_password')->
  SetAlpha('your_alpha_name');

  // Send SMS
  $SmsSenderResult = $SmsSender->SetPhone('your_phone_number')->SetText('Sms test!')->SendSms();
  echo '<pre>'; print_r($SmsSenderResult); echo '</pre>';

  // Get SMS status
  $SmsSenderResult = $SmsSender->GetSmsStatus('sms_status_id');
  echo '<pre>'; print_r($SmsSenderResult); echo '</pre>';

  // Get balance
  $SmsSenderResult = $SmsSender->GetBalance();
  echo '<pre>'; print_r($SmsSenderResult); echo '</pre>';

  // Get phone info
  $SmsSenderResult = $SmsSender->GetPhoneInfo('some_phone_number');
  echo '<pre>'; print_r($SmsSenderResult); echo '</pre>';
?>