LuosimaoGateway.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /*
  3. * This file is part of the overtrue/easy-sms.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Overtrue\EasySms\Gateways;
  11. use Overtrue\EasySms\Contracts\MessageInterface;
  12. use Overtrue\EasySms\Contracts\PhoneNumberInterface;
  13. use Overtrue\EasySms\Exceptions\GatewayErrorException;
  14. use Overtrue\EasySms\Support\Config;
  15. use Overtrue\EasySms\Traits\HasHttpRequest;
  16. /**
  17. * Class LuosimaoGateway.
  18. *
  19. * @see https://luosimao.com/docs/api/
  20. */
  21. class LuosimaoGateway extends Gateway
  22. {
  23. use HasHttpRequest;
  24. const ENDPOINT_TEMPLATE = 'https://%s.luosimao.com/%s/%s.%s';
  25. const ENDPOINT_VERSION = 'v1';
  26. const ENDPOINT_FORMAT = 'json';
  27. /**
  28. * @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
  29. * @param \Overtrue\EasySms\Contracts\MessageInterface $message
  30. * @param \Overtrue\EasySms\Support\Config $config
  31. *
  32. * @return array
  33. *
  34. * @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
  35. */
  36. public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
  37. {
  38. $endpoint = $this->buildEndpoint('sms-api', 'send');
  39. $result = $this->post($endpoint, [
  40. 'mobile' => $to->getNumber(),
  41. 'message' => $message->getContent($this),
  42. ], [
  43. 'Authorization' => 'Basic '.base64_encode('api:key-'.$config->get('api_key')),
  44. ]);
  45. if ($result['error']) {
  46. throw new GatewayErrorException($result['msg'], $result['error'], $result);
  47. }
  48. return $result;
  49. }
  50. /**
  51. * Build endpoint url.
  52. *
  53. * @param string $type
  54. * @param string $function
  55. *
  56. * @return string
  57. */
  58. protected function buildEndpoint($type, $function)
  59. {
  60. return sprintf(self::ENDPOINT_TEMPLATE, $type, self::ENDPOINT_VERSION, $function, self::ENDPOINT_FORMAT);
  61. }
  62. }