Sms.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. namespace Qiniu\Sms;
  3. use Qiniu\Auth;
  4. use Qiniu\Config;
  5. use Qiniu\Http\Error;
  6. use Qiniu\Http\Client;
  7. use Qiniu\Http\Proxy;
  8. class Sms
  9. {
  10. private $auth;
  11. private $baseURL;
  12. private $proxy;
  13. public function __construct(Auth $auth, $proxy = null, $proxy_auth = null, $proxy_user_password = null)
  14. {
  15. $this->auth = $auth;
  16. $this->baseURL = sprintf("%s/%s/", Config::SMS_HOST, Config::SMS_VERSION);
  17. $this->proxy = new Proxy($proxy, $proxy_auth, $proxy_user_password);
  18. }
  19. /**
  20. * 创建签名
  21. *
  22. * @param string $signature 签名
  23. * @param string $source 签名来源,申请签名时必须指定签名来源
  24. * @param string $pics 签名对应的资质证明图片进行 base64 编码格式转换后的字符串,可选
  25. * @return array
  26. *
  27. * @link https://developer.qiniu.com/sms/api/5844/sms-api-create-signature
  28. */
  29. public function createSignature($signature, $source, $pics = null)
  30. {
  31. $params = array();
  32. $params['signature'] = $signature;
  33. $params['source'] = $source;
  34. if (!empty($pics)) {
  35. $params['pics'] = array($this->imgToBase64($pics));
  36. }
  37. $body = json_encode($params);
  38. $url = $this->baseURL . 'signature';
  39. return $this->post($url, $body);
  40. }
  41. /**
  42. * 编辑签名
  43. *
  44. * @param string $id 签名 ID
  45. * @param string $signature 签名
  46. * @param string $source 签名来源
  47. * @param string $pics 签名对应的资质证明图片进行 base64 编码格式转换后的字符串,可选
  48. * @return array
  49. * @link https://developer.qiniu.com/sms/api/5890/sms-api-edit-signature
  50. */
  51. public function updateSignature($id, $signature, $source, $pics = null)
  52. {
  53. $params = array();
  54. $params['signature'] = $signature;
  55. $params['source'] = $source;
  56. if (!empty($pics)) {
  57. $params['pics'] = array($this->imgToBase64($pics));
  58. }
  59. $body = json_encode($params);
  60. $url = $this->baseURL . 'signature/' . $id;
  61. return $this->PUT($url, $body);
  62. }
  63. /**
  64. * 列出签名
  65. *
  66. * @param string $audit_status 审核状态:"passed"(通过), "rejected"(未通过), "reviewing"(审核中)
  67. * @param int $page 页码。默认为 1
  68. * @param int $page_size 分页大小。默认为 20
  69. * @return array
  70. * @link https://developer.qiniu.com/sms/api/5889/sms-api-query-signature
  71. */
  72. public function querySignature($audit_status = null, $page = 1, $page_size = 20)
  73. {
  74. $url = sprintf(
  75. "%s?audit_status=%s&page=%s&page_size=%s",
  76. $this->baseURL . 'signature',
  77. $audit_status,
  78. $page,
  79. $page_size
  80. );
  81. return $this->get($url);
  82. }
  83. /**
  84. * 查询单个签名
  85. *
  86. * @param string $signature_id
  87. * @return array
  88. * @link https://developer.qiniu.com/sms/api/5970/query-a-single-signature
  89. */
  90. public function checkSingleSignature($signature_id)
  91. {
  92. $url = sprintf(
  93. "%s/%s",
  94. $this->baseURL . 'signature',
  95. $signature_id
  96. );
  97. return $this->get($url);
  98. }
  99. /**
  100. * 删除签名
  101. *
  102. * @param string $signature_id 签名 ID
  103. * @return array
  104. * @link https://developer.qiniu.com/sms/api/5891/sms-api-delete-signature
  105. */
  106. public function deleteSignature($signature_id)
  107. {
  108. $url = $this->baseURL . 'signature/' . $signature_id;
  109. return $this->delete($url);
  110. }
  111. /**
  112. * 创建模板
  113. *
  114. * @param string $name 模板名称
  115. * @param string $template 模板内容 可设置自定义变量,发送短信时候使用,参考:${code}
  116. * @param string $type notification:通知类,verification:验证码,marketing:营销类,voice:语音类
  117. * @param string $description 申请理由简述
  118. * @param string $signature_id 已经审核通过的签名
  119. * @return array array
  120. * @link https://developer.qiniu.com/sms/api/5893/sms-api-create-template
  121. */
  122. public function createTemplate(
  123. $name,
  124. $template,
  125. $type,
  126. $description,
  127. $signature_id
  128. ) {
  129. $params = array();
  130. $params['name'] = $name;
  131. $params['template'] = $template;
  132. $params['type'] = $type;
  133. $params['description'] = $description;
  134. $params['signature_id'] = $signature_id;
  135. $body = json_encode($params);
  136. $url = $this->baseURL . 'template';
  137. return $this->post($url, $body);
  138. }
  139. /**
  140. * 列出模板
  141. *
  142. * @param string $audit_status 审核状态:passed (通过), rejected (未通过), reviewing (审核中)
  143. * @param int $page 页码。默认为 1
  144. * @param int $page_size 分页大小。默认为 20
  145. * @return array
  146. * @link https://developer.qiniu.com/sms/api/5894/sms-api-query-template
  147. */
  148. public function queryTemplate($audit_status = null, $page = 1, $page_size = 20)
  149. {
  150. $url = sprintf(
  151. "%s?audit_status=%s&page=%s&page_size=%s",
  152. $this->baseURL . 'template',
  153. $audit_status,
  154. $page,
  155. $page_size
  156. );
  157. return $this->get($url);
  158. }
  159. /**
  160. * 查询单个模版
  161. *
  162. * @param string $template_id 模版ID
  163. * @return array
  164. * @link https://developer.qiniu.com/sms/api/5969/query-a-single-template
  165. */
  166. public function querySingleTemplate($template_id)
  167. {
  168. $url = sprintf(
  169. "%s/%s",
  170. $this->baseURL . 'template',
  171. $template_id
  172. );
  173. return $this->get($url);
  174. }
  175. /**
  176. * 编辑模板
  177. *
  178. * @param string $id 模板 ID
  179. * @param string $name 模板名称
  180. * @param string $template 模板内容
  181. * @param string $description 申请理由简述
  182. * @param string $signature_id 已经审核通过的签名 ID
  183. * @return array
  184. * @link https://developer.qiniu.com/sms/api/5895/sms-api-edit-template
  185. */
  186. public function updateTemplate(
  187. $id,
  188. $name,
  189. $template,
  190. $description,
  191. $signature_id
  192. ) {
  193. $params = array();
  194. $params['name'] = $name;
  195. $params['template'] = $template;
  196. $params['description'] = $description;
  197. $params['signature_id'] = $signature_id;
  198. $body = json_encode($params);
  199. $url = $this->baseURL . 'template/' . $id;
  200. return $this->PUT($url, $body);
  201. }
  202. /**
  203. * 删除模板
  204. *
  205. * @param string $template_id 模板 ID
  206. * @return array
  207. * @link https://developer.qiniu.com/sms/api/5896/sms-api-delete-template
  208. */
  209. public function deleteTemplate($template_id)
  210. {
  211. $url = $this->baseURL . 'template/' . $template_id;
  212. return $this->delete($url);
  213. }
  214. /**
  215. * 发送短信
  216. *
  217. * @param string $template_id 模板 ID
  218. * @param array $mobiles 手机号
  219. * @param array $parameters 自定义模板变量,变量设置在创建模板时,参数template指定
  220. * @return array
  221. * @link https://developer.qiniu.com/sms/api/5897/sms-api-send-message
  222. */
  223. public function sendMessage($template_id, $mobiles, $parameters = null)
  224. {
  225. $params = array();
  226. $params['template_id'] = $template_id;
  227. $params['mobiles'] = $mobiles;
  228. if (!empty($parameters)) {
  229. $params['parameters'] = $parameters;
  230. }
  231. $body = json_encode($params);
  232. $url = $this->baseURL . 'message';
  233. return $this->post($url, $body);
  234. }
  235. /**
  236. * 查询发送记录
  237. *
  238. * @param string $job_id 发送任务返回的 id
  239. * @param string $message_id 单条短信发送接口返回的 id
  240. * @param string $mobile 接收短信的手机号码
  241. * @param string $status sending: 发送中,success: 发送成功,failed: 发送失败,waiting: 等待发送
  242. * @param string $template_id 模版 id
  243. * @param string $type marketing:营销,notification:通知,verification:验证码,voice:语音
  244. * @param string $start 开始时间,timestamp,例如: 1563280448
  245. * @param int $end 结束时间,timestamp,例如: 1563280471
  246. * @param int $page 页码,默认为 1
  247. * @param int $page_size 每页返回的数据条数,默认20,最大200
  248. * @return array
  249. * @link https://developer.qiniu.com/sms/api/5852/query-send-sms
  250. */
  251. public function querySendSms(
  252. $job_id = null,
  253. $message_id = null,
  254. $mobile = null,
  255. $status = null,
  256. $template_id = null,
  257. $type = null,
  258. $start = null,
  259. $end = null,
  260. $page = 1,
  261. $page_size = 20
  262. ) {
  263. $query = array();
  264. \Qiniu\setWithoutEmpty($query, 'job_id', $job_id);
  265. \Qiniu\setWithoutEmpty($query, 'message_id', $message_id);
  266. \Qiniu\setWithoutEmpty($query, 'mobile', $mobile);
  267. \Qiniu\setWithoutEmpty($query, 'status', $status);
  268. \Qiniu\setWithoutEmpty($query, 'template_id', $template_id);
  269. \Qiniu\setWithoutEmpty($query, 'type', $type);
  270. \Qiniu\setWithoutEmpty($query, 'start', $start);
  271. \Qiniu\setWithoutEmpty($query, 'end', $end);
  272. \Qiniu\setWithoutEmpty($query, 'page', $page);
  273. \Qiniu\setWithoutEmpty($query, 'page_size', $page_size);
  274. $url = $this->baseURL . 'messages?' . http_build_query($query);
  275. return $this->get($url);
  276. }
  277. public function imgToBase64($img_file)
  278. {
  279. $img_base64 = '';
  280. if (file_exists($img_file)) {
  281. $app_img_file = $img_file; // 图片路径
  282. $img_info = getimagesize($app_img_file); // 取得图片的大小,类型等
  283. $fp = fopen($app_img_file, "r"); // 图片是否可读权限
  284. if ($fp) {
  285. $filesize = filesize($app_img_file);
  286. if ($filesize > 5 * 1024 * 1024) {
  287. die("pic size < 5M !");
  288. }
  289. $img_type = null;
  290. $content = fread($fp, $filesize);
  291. $file_content = chunk_split(base64_encode($content)); // base64编码
  292. switch ($img_info[2]) { //判读图片类型
  293. case 1:
  294. $img_type = 'gif';
  295. break;
  296. case 2:
  297. $img_type = 'jpg';
  298. break;
  299. case 3:
  300. $img_type = 'png';
  301. break;
  302. }
  303. //合成图片的base64编码
  304. $img_base64 = 'data:image/' . $img_type . ';base64,' . $file_content;
  305. }
  306. fclose($fp);
  307. }
  308. return $img_base64;
  309. }
  310. private function get($url, $contentType = 'application/x-www-form-urlencoded')
  311. {
  312. $headers = $this->auth->authorizationV2($url, "GET", null, $contentType);
  313. $headers['Content-Type'] = $contentType;
  314. $ret = Client::get($url, $headers, $this->proxy->makeReqOpt());
  315. if (!$ret->ok()) {
  316. return array(null, new Error($url, $ret));
  317. }
  318. return array($ret->json(), null);
  319. }
  320. private function delete($url, $contentType = 'application/json')
  321. {
  322. $headers = $this->auth->authorizationV2($url, "DELETE", null, $contentType);
  323. $headers['Content-Type'] = $contentType;
  324. $ret = Client::delete($url, $headers, $this->proxy->makeReqOpt());
  325. if (!$ret->ok()) {
  326. return array(null, new Error($url, $ret));
  327. }
  328. return array($ret->json(), null);
  329. }
  330. private function post($url, $body, $contentType = 'application/json')
  331. {
  332. $headers = $this->auth->authorizationV2($url, "POST", $body, $contentType);
  333. $headers['Content-Type'] = $contentType;
  334. $ret = Client::post($url, $body, $headers, $this->proxy->makeReqOpt());
  335. if (!$ret->ok()) {
  336. return array(null, new Error($url, $ret));
  337. }
  338. $r = ($ret->body === null) ? array() : $ret->json();
  339. return array($r, null);
  340. }
  341. private function PUT($url, $body, $contentType = 'application/json')
  342. {
  343. $headers = $this->auth->authorizationV2($url, "PUT", $body, $contentType);
  344. $headers['Content-Type'] = $contentType;
  345. $ret = Client::put($url, $body, $headers, $this->proxy->makeReqOpt());
  346. if (!$ret->ok()) {
  347. return array(null, new Error($url, $ret));
  348. }
  349. $r = ($ret->body === null) ? array() : $ret->json();
  350. return array($r, null);
  351. }
  352. }