Sms.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. namespace Qiniu\Sms;
  3. use Qiniu\Http\Client;
  4. use Qiniu\Http\Error;
  5. use Qiniu\Config;
  6. use Qiniu\Auth;
  7. class Sms
  8. {
  9. private $auth;
  10. private $baseURL;
  11. public function __construct(Auth $auth)
  12. {
  13. $this->auth = $auth;
  14. $this->baseURL = sprintf("%s/%s/", Config::SMS_HOST, Config::SMS_VERSION);
  15. }
  16. /*
  17. * 创建签名
  18. * signature: string 类型,必填,【长度限制8个字符内】超过长度会报错
  19. * source: string 类型,必填,申请签名时必须指定签名来源。取值范围为:
  20. nterprises_and_institutions 企事业单位的全称或简称
  21. website 工信部备案网站的全称或简称
  22. app APP应用的全称或简称
  23. public_number_or_small_program 公众号或小程序的全称或简称
  24. store_name 电商平台店铺名的全称或简称
  25. trade_name 商标名的全称或简称,
  26. * pics: 本地的图片路径 string 类型,可选
  27. *@return: 类型array {
  28. "signature_id": <signature_id>
  29. }
  30. */
  31. public function createSignature($signature, $source, $pics = null)
  32. {
  33. $params['signature'] = $signature;
  34. $params['source'] = $source;
  35. if (!empty($pics)) {
  36. $params['pics'] = $this->imgToBase64($pics);
  37. }
  38. $body = json_encode($params);
  39. $url =$this->baseURL.'signature';
  40. $ret = $this->post($url, $body);
  41. return $ret;
  42. }
  43. /*
  44. * 编辑签名
  45. * id 签名id : string 类型,必填,
  46. * signature: string 类型,必填,
  47. * source: string 类型,必填,申请签名时必须指定签名来源。取值范围为:
  48. enterprises_and_institutions 企事业单位的全称或简称
  49. website 工信部备案网站的全称或简称
  50. app APP应用的全称或简称
  51. public_number_or_small_program 公众号或小程序的全称或简称
  52. store_name 电商平台店铺名的全称或简称
  53. trade_name 商标名的全称或简称,
  54. * pics: 本地的图片路径 string 类型,可选,
  55. * @return: 类型array {
  56. "signature": string
  57. }
  58. */
  59. public function updateSignature($id, $signature, $source, $pics = null)
  60. {
  61. $params['signature'] = $signature;
  62. $params['source'] = $source;
  63. if (!empty($pics)) {
  64. $params['pics'] = $this->imgToBase64($pics);
  65. }
  66. $body = json_encode($params);
  67. $url =$this->baseURL.'signature/'.$id;
  68. $ret = $this->PUT($url, $body);
  69. return $ret;
  70. }
  71. /*
  72. * 查询签名
  73. * audit_status: 审核状态 string 类型,可选,
  74. 取值范围为: "passed"(通过), "rejected"(未通过), "reviewing"(审核中)
  75. * page:页码 int 类型,
  76. * page_size: 分页大小 int 类型,可选, 默认为20
  77. *@return: 类型array {
  78. "items": [{
  79. "id": string,
  80. "signature": string,
  81. "source": string,
  82. "audit_status": string,
  83. "reject_reason": string,
  84. "created_at": int64,
  85. "updated_at": int64
  86. }...],
  87. "total": int,
  88. "page": int,
  89. "page_size": int,
  90. }
  91. */
  92. public function checkSignature($audit_status = null, $page = 1, $page_size = 20)
  93. {
  94. $url = sprintf(
  95. "%s?audit_status=%s&page=%s&page_size=%s",
  96. $this->baseURL.'signature',
  97. $audit_status,
  98. $page,
  99. $page_size
  100. );
  101. $ret = $this->get($url);
  102. return $ret;
  103. }
  104. /*
  105. * 删除签名
  106. * id 签名id string 类型,必填,
  107. * @retrun : 请求成功 HTTP 状态码为 200
  108. */
  109. public function deleteSignature($id)
  110. {
  111. $url = $this->baseURL . 'signature/' . $id;
  112. list(, $err) = $this->delete($url);
  113. return $err;
  114. }
  115. /*
  116. * 创建模板
  117. * name : 模板名称 string 类型 ,必填
  118. * template: 模板内容 string 类型,必填
  119. * type: 模板类型 string 类型,必填,
  120. 取值范围为: notification (通知类短信), verification (验证码短信), marketing (营销类短信)
  121. * description: 申请理由简述 string 类型,必填
  122. * signature_id: 已经审核通过的签名 string 类型,必填
  123. * @return: 类型 array {
  124. "template_id": string
  125. }
  126. */
  127. public function createTemplate(
  128. $name,
  129. $template,
  130. $type,
  131. $description,
  132. $signture_id
  133. ) {
  134. $params['name'] = $name;
  135. $params['template'] = $template;
  136. $params['type'] = $type;
  137. $params['description'] = $description;
  138. $params['signature_id'] = $signture_id;
  139. $body = json_encode($params);
  140. $url =$this->baseURL.'template';
  141. $ret = $this->post($url, $body);
  142. return $ret;
  143. }
  144. /*
  145. * 查询模板
  146. * audit_status: 审核状态 string 类型 ,可选,
  147. 取值范围为: passed (通过), rejected (未通过), reviewing (审核中)
  148. * page: 页码 int 类型,可选,默认为 1
  149. * page_size: 分页大小 int 类型,可选,默认为 20
  150. * @return: 类型array{
  151. "items": [{
  152. "id": string,
  153. "name": string,
  154. "template": string,
  155. "audit_status": string,
  156. "reject_reason": string,
  157. "type": string,
  158. "signature_id": string, // 模版绑定的签名ID
  159. "signature_text": string, // 模版绑定的签名内容
  160. "created_at": int64,
  161. "updated_at": int64
  162. }...],
  163. "total": int,
  164. "page": int,
  165. "page_size": int
  166. }
  167. */
  168. public function queryTemplate($audit_status = null, $page = 1, $page_size = 20)
  169. {
  170. $url = sprintf(
  171. "%s?audit_status=%s&page=%s&page_size=%s",
  172. $this->baseURL.'template',
  173. $audit_status,
  174. $page,
  175. $page_size
  176. );
  177. $ret = $this->get($url);
  178. return $ret;
  179. }
  180. /*
  181. * 编辑模板
  182. * id :模板id
  183. * name : 模板名称 string 类型 ,必填
  184. * template: 模板内容 string 类型,必填
  185. * description: 申请理由简述 string 类型,必填
  186. * signature_id: 已经审核通过的签名 string 类型,必填
  187. * @retrun : 请求成功 HTTP 状态码为 200
  188. */
  189. public function updateTemplate(
  190. $id,
  191. $name,
  192. $template,
  193. $description,
  194. $signature_id
  195. ) {
  196. $params['name'] = $name;
  197. $params['template'] = $template;
  198. $params['description'] = $description;
  199. $params['signature_id'] = $signature_id;
  200. $body = json_encode($params);
  201. $url =$this->baseURL.'template/'.$id;
  202. $ret = $this->PUT($url, $body);
  203. return $ret;
  204. }
  205. /*
  206. * 删除模板
  207. * id :模板id string 类型,必填,
  208. * @retrun : 请求成功 HTTP 状态码为 200
  209. */
  210. public function deleteTemplate($id)
  211. {
  212. $url = $this->baseURL . 'template/' . $id;
  213. list(, $err) = $this->delete($url);
  214. return $err;
  215. }
  216. /*
  217. * 发送短信
  218. * 编辑模板
  219. * template_id :模板id string类型,必填
  220. * mobiles : 手机号数组 []string 类型 ,必填
  221. * parameters: 模板内容 map[string]string 类型,可选
  222. * @return: 类型json {
  223. "job_id": string
  224. }
  225. */
  226. public function sendMessage($template_id, $mobiles, $parameters = null)
  227. {
  228. $params['template_id'] = $template_id;
  229. $params['mobiles'] = $mobiles;
  230. if (!empty($parameters)) {
  231. $params['parameters'] = $parameters;
  232. }
  233. $body = json_encode($params);
  234. $url =$this->baseURL.'message';
  235. $ret = $this->post($url, $body);
  236. return $ret;
  237. }
  238. public function imgToBase64($img_file)
  239. {
  240. $img_base64 = '';
  241. if (file_exists($img_file)) {
  242. $app_img_file = $img_file; // 图片路径
  243. $img_info = getimagesize($app_img_file); // 取得图片的大小,类型等
  244. $fp = fopen($app_img_file, "r"); // 图片是否可读权限
  245. if ($fp) {
  246. $filesize = filesize($app_img_file);
  247. if ($filesize > 5*1024*1024) {
  248. die("pic size < 5M !");
  249. }
  250. $content = fread($fp, $filesize);
  251. $file_content = chunk_split(base64_encode($content)); // base64编码
  252. switch ($img_info[2]) { //判读图片类型
  253. case 1:
  254. $img_type = 'gif';
  255. break;
  256. case 2:
  257. $img_type = 'jpg';
  258. break;
  259. case 3:
  260. $img_type = 'png';
  261. break;
  262. }
  263. //合成图片的base64编码
  264. $img_base64 = 'data:image/' . $img_type . ';base64,' . $file_content;
  265. }
  266. fclose($fp);
  267. }
  268. return $img_base64;
  269. }
  270. private function get($url, $cType = null)
  271. {
  272. $rtcToken = $this->auth->authorizationV2($url, "GET", null, $cType);
  273. $rtcToken['Content-Type'] = $cType;
  274. $ret = Client::get($url, $rtcToken);
  275. if (!$ret->ok()) {
  276. return array(null, new Error($url, $ret));
  277. }
  278. return array($ret->json(), null);
  279. }
  280. private function delete($url, $contentType = 'application/json')
  281. {
  282. $rtcToken = $this->auth->authorizationV2($url, "DELETE", null, $contentType);
  283. $rtcToken['Content-Type'] = $contentType;
  284. $ret = Client::delete($url, $rtcToken);
  285. if (!$ret->ok()) {
  286. return array(null, new Error($url, $ret));
  287. }
  288. return array($ret->json(), null);
  289. }
  290. private function post($url, $body, $contentType = 'application/json')
  291. {
  292. $rtcToken = $this->auth->authorizationV2($url, "POST", $body, $contentType);
  293. $rtcToken['Content-Type'] = $contentType;
  294. $ret = Client::post($url, $body, $rtcToken);
  295. if (!$ret->ok()) {
  296. return array(null, new Error($url, $ret));
  297. }
  298. $r = ($ret->body === null) ? array() : $ret->json();
  299. return array($r, null);
  300. }
  301. private function PUT($url, $body, $contentType = 'application/json')
  302. {
  303. $rtcToken = $this->auth->authorizationV2($url, "PUT", $body, $contentType);
  304. $rtcToken['Content-Type'] = $contentType;
  305. $ret = Client::put($url, $body, $rtcToken);
  306. if (!$ret->ok()) {
  307. return array(null, new Error($url, $ret));
  308. }
  309. $r = ($ret->body === null) ? array() : $ret->json();
  310. return array($r, null);
  311. }
  312. }