Skip to content

Commit ac66fae

Browse files
committed
Add cloud SMS interface
1 parent 90dcea9 commit ac66fae

File tree

4 files changed

+368
-0
lines changed

4 files changed

+368
-0
lines changed

docs/sms/example.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
require_once("../../autoload.php");
3+
4+
use \Qiniu\Auth;
5+
6+
$ak="xxxx";
7+
$sk="xxxx";
8+
9+
$auth = new Auth($ak, $sk);
10+
$client = new Qiniu\Sms\sms($auth);
11+
12+
//发送信息模块
13+
$template_id="1131792074274775040";
14+
$mobiles=array("18011111111","18011111111");
15+
$code = array('code' => 'code' );
16+
try {
17+
//发送短信
18+
$resp = $client->sendMessage($template_id, $mobiles, $code);
19+
print_r($resp);
20+
} catch (\Exception $e) {
21+
echo "Error:", $e, "\n";
22+
}exit;
23+
//模板模块
24+
$name="tstest001";
25+
$template="tesy001 ${code}";
26+
$type="notification";
27+
$description="tstest001";
28+
$signature_id="1131464448834277376";
29+
$id="1131810682442883072";
30+
31+
try {
32+
//创建模板
33+
$resp = $client->createTemplate($name, $template, $type, $description, $signature_id);
34+
print_r($resp);
35+
//查询模板
36+
$resp = $client->queryTemplate();
37+
print_r($resp);
38+
//修改模板
39+
$resp = $client->updateTemplate($id, $name, $template, $description, $signature_id);
40+
print_r($resp);
41+
//删除模板
42+
$resp = $client->deleteTemplate($id);
43+
print_r($resp);
44+
} catch (\Exception $e) {
45+
echo "Error:", $e, "\n";
46+
}
47+
//签名模块
48+
$signature = 'lfxlive2';
49+
$source = 'enterprises_and_institutions';
50+
$pic="/Users/Desktop/sss.jpg";
51+
$audit_status="passed";
52+
$page=1;
53+
$page_size=1;
54+
$id="1131464448834277376";
55+
56+
try {
57+
//创建签名
58+
$resp = $client->createSignature($signature, $source, $pic);
59+
print_r($resp);
60+
//查询签名
61+
$resp = $client->checkSignature($audit_status);
62+
//修改签名
63+
$resp = $client->updateSignature($id, $signature, $source, $pic);
64+
print_r($resp);
65+
//删除ID
66+
$resp = $client->deleteSignature($id);
67+
print_r($resp);
68+
} catch (\Exception $e) {
69+
echo "Error:", $e, "\n";
70+
}

src/Qiniu/Config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ final class Config
1414
const RTCAPI_HOST = 'http://rtc.qiniuapi.com';
1515
const ARGUS_HOST = 'argus.atlab.ai';
1616
const CASTER_HOST = 'pili-caster.qiniuapi.com';
17+
const SMS_HOST="https://sms.qiniuapi.com";
1718
const RTCAPI_VERSION = 'v3';
19+
const SMS_VERSION='v1';
1820

1921
// Zone 空间对应的存储区域
2022
public $region;

src/Qiniu/Http/Client.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ public static function post($url, $body, array $headers = array())
2525
return self::sendRequest($request);
2626
}
2727

28+
public static function PUT($url, $body, array $headers = array())
29+
{
30+
$request = new Request('PUT', $url, $headers, $body);
31+
return self::sendRequest($request);
32+
}
33+
2834
public static function multipartPost(
2935
$url,
3036
$fields,
@@ -56,6 +62,7 @@ public static function multipartPost(
5662
array_push($data, '');
5763

5864
$body = implode("\r\n", $data);
65+
// var_dump($data);exit;
5966
$contentType = 'multipart/form-data; boundary=' . $mimeBoundary;
6067
$headers['Content-Type'] = $contentType;
6168
$request = new Request('POST', $url, $headers, $body);
@@ -119,7 +126,13 @@ public static function sendRequest($request)
119126
curl_close($ch);
120127
return $r;
121128
}
129+
$curl_info = curl_getinfo($ch);
130+
$header_size = $curl_info["header_size"];
131+
$body = substr($result, $header_size);
122132
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
133+
if ($code != 200) {
134+
throw new \Exception("\nhttpcode:".$code."\nmessage".$body);
135+
}
123136
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
124137
$headers = self::parseHeaders(substr($result, 0, $header_size));
125138
$body = substr($result, $header_size);

src/Qiniu/Sms/sms.php

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

0 commit comments

Comments
 (0)