123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- require_once 'ApiClient.php';
- class ViberApiClient extends ApiClient {
- protected $messages = array();
- protected $sender;
- public function __construct($api_key, $sender, $source = null) {
- $this->sender = $sender;
- parent::__construct($api_key, $source);
- }
- private function getStatus($endpoint) {
- try {
- $resp = $this->sendRequest($endpoint);
- } catch (\Exception $e) {
- $error = 'Request failed (code: ' . $e->getCode() . '): ' . $e->getMessage();
- return array('error' => $error);
- }
- $result = json_decode($resp, true);
- return $result;
- }
- public function getStatusByReference($reference) {
- return $this->getStatus('viber/reference/' . $reference);
- }
- public function getStatusById($message_id) {
- return $this->getStatus('viber/' . $message_id);
- }
- public function getPrices($tariff = NULL) {
- try {
- $resp = $this->sendRequest('viber/prices' . ($tariff !== NULL ? ('/' . $tariff) : ''));
- } catch (\Exception $e) {
- $error = 'Request failed (code: ' . $e->getCode() . '): ' . $e->getMessage();
- return array('error' => $error);
- }
- $result = json_decode($resp, true);
- return $result;
- }
- public function clearMessages() {
- $this->messages = array();
- }
- /**
- * param $to is an array of ['msisdn' => $msisdn, 'reference' => $reference], where 'reference' is optional
- * @param $to
- * @param $text
- * @param $alpha_name
- * @param array $viber_options
- * @param bool $is_promotional
- * @param string $callback_url
- */
- public function addMessage($to, $text, $viber_options = array(), $alpha_name = null, $is_promotional = true, $callback_url = '') {
- $alpha_name = $alpha_name ?: $this->sender;
- $message = array();
- $message['to'] = $to;
- $message['text'] = $text;
- $message['alpha_name'] = $alpha_name;
- if (!$is_promotional)
- $message['is_promotional'] = $is_promotional;
- if ($callback_url != '')
- $message['callback_url'] = $callback_url;
- if (count($viber_options) > 0)
- $message['options']['viber'] = $viber_options;
- $this->messages[] = $message;
- }
- public function getMessagesPrice($validity = 86400, $tariff = NULL) {
- return $this->sendMessages($validity, $tariff, true);
- }
- /**
- * @param int $validity
- * @param null $tariff
- * @param bool $only_price
- * @return mixed
- */
- public function sendMessages($validity = 86400, $tariff = NULL, $only_price = false) {
- if (count($this->messages) == 0)
- return array('error' => 'No messages to send');
- $message = array();
- $message['validity'] = $validity;
- if ($tariff !== NULL)
- $message['tariff'] = $tariff;
- $message['messages'] = $this->messages;
- $endpoint = $only_price ? 'viber/price' : 'viber/create';
- try {
- $resp = $this->sendRequest($endpoint, json_encode($message), 'PUT');
- } catch (\Exception $e) {
- $error = 'Request failed (code: ' . $e->getCode() . '): ' . $e->getMessage();
- return array('error' => $error);
- }
- $result = json_decode($resp, true);
- return $result;
- }
- }
|