<?php
class SmsHostedApiAdapter extends ExternalApi {
/**
* send sms
*
* @param string $phoneNumber
* @param string $senderName - in this provider senderName is permanent
* @param string $content - message content
*
* @return string
*/
public function sendSms(string $phoneNumber, string $senderName, string $content) {
$transactionId = RandomCodeGenerator::generate(64, true);
$parameters = [
'Phone' => [$phoneNumber],
'Message' => $content,
'Sender' => $this->key,
'TransactionId' => $transactionId,
'Priority' => 1,
'FlashSms' => false,
];
return $this->sms_send($parameters);
}
/**
* real method for send sms via api
*
* @param array $params
* @param string $token
*
* @return string
*/
private function sms_send(array $parameters) {
return $this->call('Smses', $parameters, true);
}
/**
* get delivery reports
*
* @param array $parameters
*
* @return type
*/
public function getDeliveryReports(array $parameters) {
return $this->call('DeliveryReports', $parameters, false);
}
/**
* get inputs sms
*
* @param array $parameters
*
* @return type
*/
public function getInputSms(array $parameters) {
return $this->call('InputSmses', $parameters, false);
}
/**
* call method
*
* @param type $method
* @param type $parameters
* @param bool $post - flag post or get method
* @return array
*
* @throws \Exception
*/
public function call($method, $parameters = [], bool $post = true): array {
$response = [];
$url = $this->host . $method;
if (!$post) {
$parameters = http_build_query($parameters);
$url .= '?' . $parameters;
}
$parameters = json_encode($parameters);
$headers = [
'Content-Type: application/json; charset=utf-8',
'Accept: application/json',
'Authorization: Basic ' . base64_encode("$this->login:$this->pass")
];
try {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($post) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$responseJson = curl_exec($ch);
$response = json_decode($responseJson, true);
$this->lastHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
} catch (\Exception $ex) {
throw new \Exception('Curl call failed! HttpCode: ' . $this->lastHttpCode . ' (Ex: ' . $ex->getMessage() . ')');
}
return $response;
}
}