forked from hiromitz/boundio-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoundio.php
147 lines (119 loc) · 3.49 KB
/
Boundio.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/**
* boundio API simple client interface.
*
* Copyright (c) 2012, KDDI Web Communications Inc.
* All rights reserved.
*
* @package Boundio
* @author hiromitz <[email protected]>
* @link https://github.com/boundio/boundio-php
* @license http://creativecommons.org/licenses/MIT/ MIT
*/
class Boundio {
public static $_config = array(
'env' => 'develop', // develop || production
'userSerialId' => '',
'appId' => '',
'authKey' => ''
);
protected static $_baseDevUrl = 'https://boundio.jp/api/vd1/';
protected static $_baseUrl = 'https://boundio.jp/api/v1/';
public static function configure($key, $value) {
static::$_config[$key] = $value;
}
protected static function getUrl($develop=false) {
$url = (static::$_config['env'] == 'develop' && $develop === false) ? static::$_baseDevUrl : static::$_baseUrl;
$url .= static::$_config['userSerialId'];
return $url;
}
public static function call($tel_to, array $casts) {
$tel_to = str_replace('-', '', $tel_to);
// validation - phone number
if(!preg_match('/^0\d{9,10}$/', $tel_to)) {
return false;
}
// validation - casts
foreach($casts as $cast) {
if(!preg_match('/^(file\([0-9]+\)|num\([0-9]\)|silent\(\))$/', $cast)) {
return false;
}
}
// create cast string
$cast = implode('%%', $casts);
// execute call
$result = static::_execute(static::getUrl(). '/call', array(
'tel_to' => $tel_to,
'cast' => $cast
), 'post');
$result = json_decode($result, true);
return $result;
}
public static function status($id='', $start='', $end='', $count=100) {
$start = preg_replace('/[-\/]/', '', $start);
$end = preg_replace('/[-\/]/', '', $end);
$params = array();
if($id !== '') {
$params['tel_id'] = $id;
} elseif($start !== '') {
// search one day if end day is not given
if($end === '') {
$end = $start;
}
$params['start'] = $start;
$params['end'] = $end;
}
// execute get status
$result = static::_execute(static::getUrl(). '/tel_status', $params);
$result = json_decode($result, true);
return $result;
}
public static function file($text='', $file='', $filename) {
$params = array();
$options = array();
if($text !== '') {
$params['convtext'] = $text;
$params['filename'] = $filename;
} else {
// file validation
if(!file_exists($file)) {
return false;
}
$params['file'] = '@'. $file;
$params['filename'] = $filename;
}
// execute get status
$result = static::_execute(static::getUrl(true). '/file/post', $params, 'post');
$result = json_decode($result, true);
return $result;
}
protected static function _execute($url, array $params, $method='get', array $options = array()) {
$params['auth'] = static::$_config['authKey'];
$params['key'] = static::$_config['appId'];
$defaults = ($method == 'post') ? array(
CURLOPT_POST => ($method == 'post') ? 1: 0,
CURLOPT_HEADER => 0,
CURLOPT_URL => $url,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => 4,
CURLOPT_POSTFIELDS => $params
) : array(
CURLOPT_URL => $url. (strpos($url, '?') === FALSE ? '?' : ''). http_build_query($params),
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_TIMEOUT => 4
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
/**
* BoundioException class
*
*/
class BoundioException extends Exception{}