LuosimaoGateway.php 1.8 KB
Newer Older
J
jcc 已提交
1 2 3 4
<?php

/*
 * This file is part of the overtrue/easy-sms.
O
overtrue 已提交
5
 * (c) overtrue <i@overtrue.me>
J
jcc 已提交
6 7 8 9 10 11
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Overtrue\EasySms\Gateways;

O
overtrue 已提交
12 13 14 15
use Overtrue\EasySms\Contracts\MessageInterface;
use Overtrue\EasySms\Exceptions\GatewayErrorException;
use Overtrue\EasySms\Support\Config;
use Overtrue\EasySms\Traits\HasHttpRequest;
J
jcc 已提交
16 17 18

/**
 * Class LuosimaoGateway.
O
overtrue 已提交
19 20
 *
 * @see https://luosimao.com/docs/api/
J
jcc 已提交
21 22 23 24 25 26 27 28 29 30
 */
class LuosimaoGateway extends Gateway
{
    use HasHttpRequest;

    const ENDPOINT_TEMPLATE = 'https://%s.luosimao.com/%s/%s.%s';
    const ENDPOINT_VERSION = 'v1';
    const ENDPOINT_FORMAT = 'json';

    /**
O
overtrue 已提交
31 32 33
     * @param array|int|string                             $to
     * @param \Overtrue\EasySms\Contracts\MessageInterface $message
     * @param \Overtrue\EasySms\Support\Config             $config
J
jcc 已提交
34
     *
O
CS.  
overtrue 已提交
35
     * @return array
O
overtrue 已提交
36 37
     *
     * @throws \Overtrue\EasySms\Exceptions\GatewayErrorException;
J
jcc 已提交
38
     */
O
overtrue 已提交
39
    public function send($to, MessageInterface $message, Config $config)
J
jcc 已提交
40 41 42
    {
        $endpoint = $this->buildEndpoint('sms-api', 'send');

O
overtrue 已提交
43
        $result = $this->post($endpoint, [
J
jcc 已提交
44
            'mobile' => $to,
O
overtrue 已提交
45
            'message' => $message->getContent(),
J
jcc 已提交
46
        ], [
O
overtrue 已提交
47
            'Authorization' => 'Basic '.base64_encode('api:key-'.$config->get('api_key')),
J
jcc 已提交
48
        ]);
O
overtrue 已提交
49 50 51 52 53 54

        if ($result['error']) {
            throw new GatewayErrorException($result['msg'], $result['error'], $result);
        }

        return $result;
J
jcc 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    }

    /**
     * Build endpoint url.
     *
     * @param string $type
     * @param string $function
     *
     * @return string
     */
    protected function buildEndpoint($type, $function)
    {
        return sprintf(self::ENDPOINT_TEMPLATE, $type, self::ENDPOINT_VERSION, $function, self::ENDPOINT_FORMAT);
    }
}