ChuanglanSmsApi.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace app\common\api;
  3. class ChuanglanSmsApi{
  4. //参数的配置请登录zz.253.com获取以下API信息↓↓↓↓↓↓↓
  5. const API_SEND_URL='http://smssh1.253.com/msg/send/json'; //创蓝发送短信接口URL
  6. const API_ACCOUNT = 'N7450217'; //创蓝API账号
  7. const API_PASSWORD = 'wS5gE4GQszb46f';//创蓝API密码
  8. private $verification;
  9. /**
  10. * 发送短信
  11. *
  12. * @param string $mobile 手机号码
  13. * @param string $msg 短信内容
  14. * @param string $needstatus 是否需要状态报告
  15. */
  16. public function sendSMS( $mobile, $msg, $needstatus = 'true') {
  17. //创蓝接口参数
  18. $postArr = array (
  19. 'account' => self::API_ACCOUNT,
  20. 'password' => self::API_PASSWORD,
  21. 'msg' => urlencode($msg),
  22. 'phone' => $mobile,
  23. 'report' => $needstatus,
  24. );
  25. $result = $this->curlPost( self::API_SEND_URL, $postArr);
  26. return $result;
  27. }
  28. /**
  29. * 通过CURL发送HTTP请求
  30. * @param string $url //请求URL
  31. * @param array $postFields //请求参数
  32. * @return mixed
  33. *
  34. */
  35. private function curlPost($url,$postFields){
  36. $postFields = json_encode($postFields);
  37. $ch = curl_init ();
  38. curl_setopt( $ch, CURLOPT_URL, $url );
  39. curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
  40. 'Content-Type: application/json; charset=utf-8' //json版本需要填写 Content-Type: application/json;
  41. )
  42. );
  43. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码
  44. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
  45. curl_setopt( $ch, CURLOPT_POST, 1 );
  46. curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields);
  47. curl_setopt( $ch, CURLOPT_TIMEOUT,60);
  48. curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
  49. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
  50. $ret = curl_exec ( $ch );
  51. if (false == $ret) {
  52. $result = curl_error( $ch);
  53. } else {
  54. $rsp = curl_getinfo( $ch, CURLINFO_HTTP_CODE);
  55. if (200 != $rsp) {
  56. $result = "请求状态 ". $rsp . " " . curl_error($ch);
  57. } else {
  58. $result = $ret;
  59. }
  60. }
  61. curl_close ( $ch );
  62. return $result;
  63. }
  64. }