123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- namespace Aix\Sms;
- use Aix\Sms\Contracts\Smser;
- use App\Models\SmsTemplate;
- use GuzzleHttp\Client;
- use Illuminate\Support\Facades\Cache;
- /**
- * 创蓝短信
- * Class AliSms
- * @package Aix\Sms
- * Auth Zhong
- * Date 2018/11/2
- */
- class ChuanglanSms implements Smser
- {
- /**
- * @var DefaultAcsClient
- */
- private $api_account;
- private $api_password;
- private $httpClient;
- private $api_send_url = 'http://smssh1.253.com/msg/variable/json';
- private $api_send_url_common = 'http://smssh1.253.com/msg/send/json';
- /**
- * SmsService constructor.
- * @param $app_key
- * @param $app_secret
- * @param $sign_key
- */
- public function __construct($app_key, $app_secret, $sign_key)
- {
- $this->api_account = $app_key;
- $this->api_password = $app_secret;
- $this->sign_key = $sign_key;
- $this->httpClient=new Client([
- 'http_errors' => false
- ]);
- }
- /**
- * 发送基本短信
- * @param string $mobile
- * @param SmsTemplate $template
- * @param array $params
- * @return bool
- * @throws \Exception
- */
- public function sendSms(string $mobile, SmsTemplate $template, array $params = [],$isVariable = true, $content = ''): bool
- {
- if(!$isVariable){
- $params_str = $mobile;
- $url = $this->api_send_url_common;
- //创蓝接口参数
- $postArr = array (
- 'account' => $this->api_account,
- 'password' => $this->api_password,
- 'msg' => $content,
- 'phone' => $params_str,
- 'report' => 'true'
- );
- //error_log("无变量发送参数:".json_encode($postArr)."\r\n",3,'/data/wwwroot/jucai/sms');
- }else{
- $params_str = $mobile . ',' . implode(',',$params);
- $url = $this->api_send_url;
- //创蓝接口参数
- $postArr = array (
- 'account' => $this->api_account,
- 'password' => $this->api_password,
- 'msg' => $template->value,
- 'params' => $params_str,
- 'report' => 'true'
- );
- //error_log("有变量发送参数:".json_encode($postArr)."\r\n",3,'/data/wwwroot/jucai/sms');
- }
- $postFields = json_encode($postArr);
- $ch = curl_init ();
- curl_setopt( $ch, CURLOPT_URL, $url );
- curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json; charset=utf-8' //json版本需要填写 Content-Type: application/json;
- )
- );
- curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码
- curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
- curl_setopt( $ch, CURLOPT_POST, 1 );
- curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields);
- curl_setopt( $ch, CURLOPT_TIMEOUT,60);
- curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
- $ret = curl_exec ( $ch );
- curl_close ( $ch );
- //error_log("返回结果:".json_encode($ret)."\r\n",3,'/data/wwwroot/jucai/sms');
- return json_encode($ret);
- }
- /**
- * 发送确认短信,需要缓存验证码code
- * @param string $mobile
- * @param SmsTemplate $template
- * @return bool
- * @throws \Exception
- */
- public function sendAuthSms(string $mobile, SmsTemplate $template): bool
- {
- $params['code']=rand(100000, 999999);
- //error_log("{$mobile}-{$params['code']}",3,'/data/wwwroot/jucai/sms');
- $ret = $this->sendSms($mobile, $template, $params);
- Cache::put($template->alias.'_'.$mobile, $params['code'], 15);
- return $ret;
- }
- /**
- * 批量发送短信
- * @param array $mobiles
- * @param SmsTemplate $template
- * @param array $params
- * @return bool
- */
- public function sendBatchSms(array $mobiles, SmsTemplate $template, array $params = []): bool
- {
- return true;
- }
- }
|