|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* docomo雑談対話APIメインクラス |
|
4
|
|
|
* @author AIZAWA Hina <[email protected]> |
|
5
|
|
|
* @copyright 2015 by AIZAWA Hina <[email protected]> |
|
6
|
|
|
* @license https://github.com/fetus-hina/docomo-dialogue/blob/master/LICENSE MIT |
|
7
|
|
|
* @since 0.1.0 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace jp3cki\docomoDialogue; |
|
11
|
|
|
|
|
12
|
|
|
use \Curl\Curl; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* docomo雑談対話APIメインクラス |
|
16
|
|
|
* |
|
17
|
|
|
* @property-read string $apikey docomo API Key |
|
18
|
|
|
* @property-read RequestParameter $parameter 送信パラメータ管理クラス |
|
19
|
|
|
*/ |
|
20
|
|
|
class Dialogue |
|
21
|
|
|
{ |
|
22
|
|
|
/** docomo側エンドポイントURL */ |
|
23
|
|
|
const END_POINT_URL = 'https://api.apigw.smt.docomo.ne.jp/dialogue/v1/dialogue'; |
|
24
|
|
|
|
|
25
|
|
|
/** @internal @var string */ |
|
26
|
|
|
private $apikey; |
|
27
|
|
|
|
|
28
|
|
|
/** @internal @var RequestParameter */ |
|
29
|
|
|
private $request; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* コンストラクタ |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $apikey docomo API Key |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct($apikey) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->apikey = $apikey; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* マジックメソッド __get |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $key プロパティ名 |
|
45
|
|
|
* @return mixed |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __get($key) |
|
48
|
|
|
{ |
|
49
|
|
|
switch ($key) { |
|
50
|
|
|
case 'apikey': |
|
51
|
|
|
return $this->apikey; |
|
52
|
|
|
case 'parameter': |
|
53
|
|
|
return $this->getParameter(); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* 送信パラメータ管理クラスを取得 |
|
59
|
|
|
* |
|
60
|
|
|
* @return RequestParameter |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getParameter() |
|
63
|
|
|
{ |
|
64
|
|
|
if (!$this->request) { |
|
65
|
|
|
$this->request = new RequestParameter(); |
|
66
|
|
|
} |
|
67
|
|
|
return $this->request; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* docomoへリクエストを送信し、対話内容を得る |
|
72
|
|
|
* |
|
73
|
|
|
* @return Response|false |
|
74
|
|
|
*/ |
|
75
|
|
|
public function request() |
|
76
|
|
|
{ |
|
77
|
|
|
$reqUrl = sprintf('%s?APIKEY=%s', self::END_POINT_URL, rawurlencode($this->apikey)); |
|
78
|
|
|
$reqBody = json_encode($this->getParameter()->makeParameter()); |
|
79
|
|
|
|
|
80
|
|
|
$curl = new Curl(); |
|
81
|
|
|
$curl->setHeader('Content-Type', 'application/json; charset=UTF-8'); |
|
82
|
|
|
$ret = $curl->post($reqUrl, $reqBody); |
|
|
|
|
|
|
83
|
|
|
if ($curl->error) { |
|
84
|
|
|
trigger_error('docomo dialogue: Error ' . $curl->errorCode . ': ' . $curl->errorMessage, E_USER_WARNING); |
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
return new Response($ret); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* クラス名(FQCN)を取得 |
|
92
|
|
|
* |
|
93
|
|
|
* return string |
|
94
|
|
|
*/ |
|
95
|
|
|
public static function className() |
|
96
|
|
|
{ |
|
97
|
|
|
return get_called_class(); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: