Chuanglan.php 2.4 KB

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