Chuanglan.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * 手机短信类
  4. */
  5. namespace chuanglan;
  6. use app\common\model\SettingModel;
  7. use think\facade\Log;
  8. class Chuanglan
  9. {
  10. /**
  11. * 创蓝短信
  12. * @param $mobile
  13. * @param $smslog_param
  14. * @return array
  15. */
  16. public function send($mobile, $smslog_param)
  17. {
  18. $appkey = SettingModel::getConfigValue('sms_chuanglan_appkey');
  19. $secret = SettingModel::getConfigValue('sms_chuanglan_secret');
  20. if (empty($appkey) || empty($secret)) {
  21. return ['code' => 1, 'msg' => '请绑定模板参数'];
  22. }
  23. if (empty($smslog_param['params'])) {
  24. $url = 'http://smssh1.253.com/msg/send/json';
  25. //创蓝接口参数
  26. $postArr = [
  27. 'account' => $appkey,
  28. 'password' => $secret,
  29. 'msg' => $smslog_param['message'],
  30. 'phone' => $mobile,
  31. 'report' => 'true',
  32. ];
  33. } else {
  34. $params_str = $mobile . ',' . implode(',', $smslog_param['params']);
  35. $url = 'http://smssh1.253.com/msg/variable/json';
  36. //创蓝接口参数
  37. $postArr = [
  38. 'account' => $appkey,
  39. 'password' => $secret,
  40. 'msg' => $smslog_param['message'],
  41. 'params' => $params_str,
  42. 'report' => 'true',
  43. ];
  44. }
  45. $postFields = json_encode($postArr);
  46. $ch = curl_init();
  47. curl_setopt($ch, CURLOPT_URL, $url);
  48. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  49. 'Content-Type: application/json; charset=utf-8' //json版本需要填写 Content-Type: application/json;
  50. ]
  51. );
  52. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码
  53. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  54. curl_setopt($ch, CURLOPT_POST, 1);
  55. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  56. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  57. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  58. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  59. $ret = curl_exec($ch);
  60. curl_close($ch);
  61. $rsp = json_decode($ret, true);
  62. if ($rsp['code'] != 0) {
  63. Log::error('短信发送失败:' . json_encode($rsp) . "。原始参数:" . json_encode($postArr));
  64. return ['code' => 1, 'msg' => $rsp['errorMsg']];
  65. } else {
  66. return ['code' => 0];
  67. }
  68. }
  69. }