diff --git a/src/Handlers/Sendcloud.php b/src/Handlers/Sendcloud.php new file mode 100644 index 0000000..0a770f2 --- /dev/null +++ b/src/Handlers/Sendcloud.php @@ -0,0 +1,93 @@ + + * @author Bill Li + * @package AnomalyLab\LuminousSMS\Handlers\Sendcloud + */ +class Sendcloud extends Handler +{ + use HasHttpRequest; + + const REQUEST_TEMPLATE = 'http://www.sendcloud.net/smsapi/%s'; + + /** + * The handler name. + * + * @var string + */ + protected $name = 'sendcloud'; + + /** + * Seed message. + * + * The current drive service providers to implement push information content. + * + * @param \AnomalyLab\LuminousSMS\Contracts\MessagerInterface $messager + * + * @return array + * + * @throws \AnomalyLab\LuminousSMS\Exceptions\HandlerBadException; + */ + public function send(MessagerInterface $messager) : array + { + $params = [ + 'smsUser' => Arrays::get($this->config, 'sms_user'), + 'templateId' => $message->getTemplate($this), + 'phone' => is_array($to) ? implode(',', $to) : $to, + 'vars' => $this->formatTemplateVars($message->getData($this)), + ]; + + if ( $config->get('timestamp', false) ) + { + $params['timestamp'] = time() * 1000; + } + + $params['signature'] = $this->sign($params, Arrays::get($this->config, 'sms_key')); + $result = $this->post(sprintf(self::REQUEST_TEMPLATE, 'send'), $params); + + if ( !$result['result'] ) + { + throw new HandlerBadException($result['message'], $result['statusCode'], $result); + } + return $result; + } + + /** + * @param array $vars + * + * @return string + */ + protected function formatTemplateVars(array $vars) : string + { + $formatted = []; + foreach ( $vars as $key => $value ) + { + $formatted[sprintf('%%%s%%', trim($key, '%'))] = $value; + } + + return json_encode($formatted, JSON_FORCE_OBJECT); + } + + /** + * @param array $params + * @param string $key + * + * @return string + */ + protected function sign($params, $key) + { + ksort($params); + return md5(sprintf('%s&%s&%s', $key, urldecode(http_build_query($params)), $key)); + } +} \ No newline at end of file diff --git a/src/LuminousSms.php b/src/LuminousSms.php index 5d55921..3c7509a 100644 --- a/src/LuminousSms.php +++ b/src/LuminousSms.php @@ -11,6 +11,7 @@ Handlers\Yunpian, Handlers\Juhe, Handlers\Alidayu, + Handlers\Sendcloud, Handlers\Baidu }; @@ -41,7 +42,8 @@ class LuminousSMS 'yunpian' => Yunpian::class, 'alidayu' => Alidayu::class, 'baidu' => Baidu::class, - 'juhe' => Juhe::class + 'juhe' => Juhe::class, + 'sendcloud' => Sendcloud::class ]; /**