回答編集履歴

6

クラス作ってみた

2016/03/12 18:56

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -32,20 +32,26 @@
32
32
 
33
33
 
34
34
 
35
+ ### SpikeException.php
36
+
37
+
38
+
35
39
  ```php
36
40
 
37
41
  <?php
38
42
 
39
- require __DIR__ .'/../const.php';
43
+
40
-
41
-
42
-
44
+
43
- class APIException extends \RuntimeException {
45
+ class SpikeException extends \RuntimeException {
46
+
47
+
44
48
 
45
49
  private $type;
46
50
 
47
51
  private $status;
48
52
 
53
+
54
+
49
55
  public function __construct($message, $type, $status) {
50
56
 
51
57
  parent::__construct($message);
@@ -56,79 +62,143 @@
56
62
 
57
63
  }
58
64
 
65
+
66
+
59
67
  public function __toString() {
60
68
 
61
69
  return "[$this->status] $this->type: {$this->getMessage()}";
62
70
 
63
71
  }
64
72
 
73
+
74
+
65
75
  public function getType() {
66
76
 
67
77
  return $this->type;
68
78
 
69
79
  }
70
80
 
81
+
82
+
71
83
  public function getStatusCode() {
72
84
 
73
85
  return $this->status;
74
86
 
75
87
  }
76
88
 
89
+
90
+
77
91
  }
78
92
 
79
-
93
+ ```
94
+
95
+
96
+
80
-
97
+ ### SpikeClient.php
98
+
99
+
100
+
101
+ ```php
102
+
103
+ <?php
104
+
105
+
106
+
107
+ class SpikeClient {
108
+
109
+
110
+
111
+ private $apikey;
112
+
113
+ private $baseurl;
114
+
115
+
116
+
117
+ public function __construct($apikey, $baseurl = 'https://api.spike.cc/v1') {
118
+
119
+ $this->apikey = $apikey;
120
+
121
+ $this->baseurl = $baseurl;
122
+
123
+ }
124
+
125
+
126
+
81
- function call_api(array $params) {
127
+ public function post($endpoint, array $params = []) {
82
-
128
+
83
- $ch = curl_init();
129
+ $ch = curl_init();
84
-
130
+
85
- curl_setopt_array($ch, [
131
+ curl_setopt_array($ch, [
86
-
132
+
87
- CURLOPT_URL => API_URL . 'tokens',
133
+ CURLOPT_URL => "$this->baseurl$endpoint",
88
-
134
+
89
- CURLOPT_USERPWD => API_KEY . ':',
135
+ CURLOPT_USERPWD => "$this->apikey:",
90
-
136
+
91
- CURLOPT_RETURNTRANSFER => true,
137
+ CURLOPT_RETURNTRANSFER => true,
92
-
138
+
93
- CURLOPT_POST => true,
139
+ CURLOPT_POST => true,
94
-
140
+
95
- CURLOPT_POSTFIELDS => http_build_query($params, '', '&'),
141
+ CURLOPT_POSTFIELDS => http_build_query($params, '', '&'),
96
-
142
+
97
- ]);
143
+ ]);
98
-
144
+
99
- $r = json_decode(curl_exec($ch));
145
+ $r = json_decode(curl_exec($ch));
100
-
146
+
101
- $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
147
+ $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
102
-
148
+
103
- if (curl_errno($ch)) {
149
+ if (curl_errno($ch)) {
104
-
150
+
105
- throw new APIException(curl_error($ch), 'curl_error', 0);
151
+ throw new SpikeException(curl_error($ch), 'curl_error', 0);
106
-
107
- }
108
-
109
- if ($status >= 400) {
110
-
111
- if (isset($r->error)) {
112
-
113
- throw new APIException($r->error->message, $r->error->type, $status);
114
152
 
115
153
  }
116
154
 
155
+ if ($status >= 400) {
156
+
157
+ if (isset($r->error)) {
158
+
117
- throw new APIException('不明なエラー', 'unknown', $status);
159
+ throw new SpikeException($r->error->message, $r->error->type, $status);
118
-
160
+
119
- }
161
+ }
162
+
120
-
163
+ throw new SpikeException('不明なエラー', 'unknown', $status);
164
+
165
+ }
166
+
121
- return $r;
167
+ return $r;
168
+
169
+ }
170
+
171
+
122
172
 
123
173
  }
124
174
 
175
+ ```
176
+
177
+
178
+
179
+ ### main.php
180
+
181
+
182
+
183
+ ```php
184
+
185
+ <?php
186
+
187
+
188
+
189
+ require_once __DIR__ . '/../SpikeClient.php';
190
+
191
+ require_once __DIR__ . '/../SpikeExeption.php';
192
+
125
193
 
126
194
 
127
195
  try {
128
196
 
129
197
 
130
198
 
199
+ $client = new SpikeClient('XXXXX');
200
+
131
- var_dump(call_api([
201
+ var_dump($client->post('/tokens', [
132
202
 
133
203
  'card' => [
134
204
 
@@ -152,7 +222,7 @@
152
222
 
153
223
 
154
224
 
155
- } catch (APIException $e) {
225
+ } catch (SpikeException $e) {
156
226
 
157
227
 
158
228
 

5

いろいろ追記

2016/03/12 18:56

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -1,4 +1,6 @@
1
+ - ライブラリや設定ファイルを読み込む用途では, `include` より `require` のほうが適切です.また括弧は省略可能です.
2
+
1
- - PHP5.3以降は `dirname(__FILE__)` の代わりに `__DIR__` が使えます. またこの用途では `include` より `require` のほうが適切です.
3
+ - PHP5.3以降は `dirname(__FILE__)` の代わりに `__DIR__` が使えます.
2
4
 
3
5
  - **Content-Typeおよびパラメータの作り方が間違っています. 送信形式は`application/json`ではなく`application/x-www-form-urlencoded`です. なお,デフォルトでこの値が使わるので明示的に設定する必要はありません.**
4
6
 
@@ -10,11 +12,19 @@
10
12
 
11
13
  - HTTPバージョンはほとんどの場合デフォルトで1.1なので明示的に指定する必要はありません.ちなみに1.0であったとしても正しく処理されると思います.
12
14
 
15
+ - 配列が複数行に渡る場合は,末尾要素にも`,`を付けましょう,このことは[マニュアルが推奨しています](http://www.php.net/manual/ja/language.types.array.php).
16
+
17
+ - POSTされるデータはネストした配列としても表現でき,こちらのほうが読みやすくなるはずです.
18
+
13
- - POSTされるデータはネストした配列としても表現でき,こちらのほうが読みやすくなるはずです.またクエリの組み立てには`http_build_query`を使いましょう.この関数の動作は引数を省略するとphp.iniに依存するので,第3引数まで必ず指定しましょう.
19
+ - クエリの組み立てには`http_build_query`を使いましょう.この関数の動作は引数を省略するとphp.iniに依存するので,第3引数まで必ず指定しましょう.
14
20
 
15
21
  - **カード番号は文字列で表現すべきです.32bit環境で動作させると整数がオーバーフローする可能性があります.**
16
22
 
17
23
  - `json_decode`は第2引数に`true`を指定せずにオブジェクトとして扱うのがおすすめです. 配列なら `$a['b']['c'][0]['d'][1]` とアクセスするところをオブジェクトなら `$a->b->c[0]->d[1]` と表現でき,こちらのほうが読みやすく書きやすいです.
24
+
25
+ - 結果の確認には `print_r` よりも `var_dump` を使いましょう. `print_r` は型が判別出来ないので不正確なデバッグ情報しか得られません.
26
+
27
+ - HTMLが後ろに続かない場合 `?>` は省略しましょう.このことは[マニュアルが推奨しています](http://www.php.net/manual/ja/language.basic-syntax.phptags.php).
18
28
 
19
29
 
20
30
 

4

ステータスコードの判定を変更

2016/03/12 16:43

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -96,7 +96,7 @@
96
96
 
97
97
  }
98
98
 
99
- if ($status !== 200) {
99
+ if ($status >= 400) {
100
100
 
101
101
  if (isset($r->error)) {
102
102
 

3

エラー処理を追加

2016/03/12 16:32

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -18,6 +18,10 @@
18
18
 
19
19
 
20
20
 
21
+ せっかくなのでエラー処理とかもキッチリ書いてみました.
22
+
23
+
24
+
21
25
  ```php
22
26
 
23
27
  <?php
@@ -26,19 +30,95 @@
26
30
 
27
31
 
28
32
 
29
- $ch = curl_init();
33
+ class APIException extends \RuntimeException {
30
34
 
31
- curl_setopt_array($ch, [
35
+ private $type;
32
36
 
33
- CURLOPT_URL => API_URL . 'tokens',
37
+ private $status;
34
38
 
35
- CURLOPT_USERPWD => API_KEY . ':',
39
+ public function __construct($message, $type, $status) {
36
40
 
37
- CURLOPT_RETURNTRANSFER => true,
41
+ parent::__construct($message);
38
42
 
39
- CURLOPT_POST => true,
43
+ $this->type = $type;
40
44
 
45
+ $this->status = $status;
46
+
47
+ }
48
+
49
+ public function __toString() {
50
+
51
+ return "[$this->status] $this->type: {$this->getMessage()}";
52
+
53
+ }
54
+
55
+ public function getType() {
56
+
57
+ return $this->type;
58
+
59
+ }
60
+
61
+ public function getStatusCode() {
62
+
63
+ return $this->status;
64
+
65
+ }
66
+
67
+ }
68
+
69
+
70
+
71
+ function call_api(array $params) {
72
+
73
+ $ch = curl_init();
74
+
75
+ curl_setopt_array($ch, [
76
+
77
+ CURLOPT_URL => API_URL . 'tokens',
78
+
79
+ CURLOPT_USERPWD => API_KEY . ':',
80
+
81
+ CURLOPT_RETURNTRANSFER => true,
82
+
41
- CURLOPT_POSTFIELDS => http_build_query([
83
+ CURLOPT_POST => true,
84
+
85
+ CURLOPT_POSTFIELDS => http_build_query($params, '', '&'),
86
+
87
+ ]);
88
+
89
+ $r = json_decode(curl_exec($ch));
90
+
91
+ $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
92
+
93
+ if (curl_errno($ch)) {
94
+
95
+ throw new APIException(curl_error($ch), 'curl_error', 0);
96
+
97
+ }
98
+
99
+ if ($status !== 200) {
100
+
101
+ if (isset($r->error)) {
102
+
103
+ throw new APIException($r->error->message, $r->error->type, $status);
104
+
105
+ }
106
+
107
+ throw new APIException('不明なエラー', 'unknown', $status);
108
+
109
+ }
110
+
111
+ return $r;
112
+
113
+ }
114
+
115
+
116
+
117
+ try {
118
+
119
+
120
+
121
+ var_dump(call_api([
42
122
 
43
123
  'card' => [
44
124
 
@@ -58,10 +138,18 @@
58
138
 
59
139
  'email' => 'foo@example.com',
60
140
 
61
- ], '', '&'),
141
+ ]));
62
142
 
63
- ]);
64
143
 
144
+
145
+ } catch (APIException $e) {
146
+
147
+
148
+
65
- var_dump(json_decode(curl_exec($ch)));
149
+ var_dump((string)$e);
150
+
151
+
152
+
153
+ }
66
154
 
67
155
  ```

2

email

2016/03/12 16:27

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -56,7 +56,7 @@
56
56
 
57
57
  'currency' => 'JPY',
58
58
 
59
- 'email' => 'example.com',
59
+ 'email' => 'foo@example.com',
60
60
 
61
61
  ], '', '&'),
62
62
 

1

引数指定忘れ

2016/03/12 16:12

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -58,7 +58,7 @@
58
58
 
59
59
  'email' => 'example.com',
60
60
 
61
- ]),
61
+ ], '', '&'),
62
62
 
63
63
  ]);
64
64