1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. |
4
|
|
|
* |
5
|
|
|
* You are hereby granted a non-exclusive, worldwide, royalty-free license to |
6
|
|
|
* use, copy, modify, and distribute this software in source code or binary |
7
|
|
|
* form for use in connection with the web services and APIs provided by |
8
|
|
|
* Facebook. |
9
|
|
|
* |
10
|
|
|
* As with any software that integrates with the Facebook platform, your use |
11
|
|
|
* of this software is subject to the Facebook Developer Principles and |
12
|
|
|
* Policies [http://developers.facebook.com/policy/]. This copyright notice |
13
|
|
|
* shall be included in all copies or substantial portions of the software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
20
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
21
|
|
|
* DEALINGS IN THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace FacebookAds; |
26
|
|
|
|
27
|
|
|
use FacebookAds\Http\Client; |
28
|
|
|
use FacebookAds\Http\RequestInterface; |
29
|
|
|
use FacebookAds\Http\ResponseInterface; |
30
|
|
|
use FacebookAds\Logger\LoggerInterface; |
31
|
|
|
use FacebookAds\Logger\NullLogger; |
32
|
|
|
use FacebookAds\Http\Exception\RequestException; |
33
|
|
|
|
34
|
|
|
class Api { |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
const VERSION = ApiConfig::APIVersion; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Api |
43
|
|
|
*/ |
44
|
|
|
protected static $instance; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Session |
48
|
|
|
*/ |
49
|
|
|
private $session; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var LoggerInterface |
53
|
|
|
*/ |
54
|
|
|
protected $logger; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var Client |
58
|
|
|
*/ |
59
|
|
|
protected $httpClient; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
protected $defaultGraphVersion; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param Client $http_client |
68
|
|
|
* @param Session $session A Facebook API session |
69
|
|
|
*/ |
70
|
6 |
|
public function __construct( |
71
|
|
|
Client $http_client, |
72
|
|
|
Session $session) { |
73
|
6 |
|
$this->httpClient = $http_client; |
74
|
6 |
|
$this->session = $session; |
75
|
6 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $app_id |
79
|
|
|
* @param string $app_secret |
80
|
|
|
* @param string $access_token |
81
|
|
|
* @return static |
82
|
|
|
*/ |
83
|
1 |
|
public static function init($app_id, $app_secret, $access_token) { |
84
|
1 |
|
$session = new Session($app_id, $app_secret, $access_token); |
85
|
1 |
|
$api = new static(new Client(), $session); |
86
|
1 |
|
static::setInstance($api); |
87
|
|
|
|
88
|
1 |
|
return $api; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return Api|null |
93
|
|
|
*/ |
94
|
26 |
|
public static function instance() { |
95
|
26 |
|
return static::$instance; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param Api $instance |
100
|
|
|
*/ |
101
|
2 |
|
public static function setInstance(Api $instance) { |
102
|
2 |
|
static::$instance = $instance; |
103
|
2 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $string |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
1 |
|
public static function base64UrlEncode($string) { |
110
|
1 |
|
$str = strtr(base64_encode($string), '+/', '-_'); |
111
|
1 |
|
$str = str_replace('=', '', $str); |
112
|
1 |
|
return $str; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $path |
117
|
|
|
* @param string $method |
118
|
|
|
* @param array $params |
119
|
|
|
* @return RequestInterface |
120
|
|
|
*/ |
121
|
1 |
|
public function prepareRequest( |
122
|
|
|
$path, |
123
|
|
|
$method = RequestInterface::METHOD_GET, |
124
|
|
|
array $params = array()) { |
125
|
|
|
|
126
|
1 |
|
$request = $this->getHttpClient()->createRequest(); |
127
|
1 |
|
$request->setMethod($method); |
128
|
1 |
|
$request->setGraphVersion($this->getDefaultGraphVersion()); |
129
|
1 |
|
$request->setPath($path); |
130
|
|
|
|
131
|
1 |
|
if ($method === RequestInterface::METHOD_GET) { |
132
|
1 |
|
$params_ref = $request->getQueryParams(); |
133
|
1 |
|
} else { |
134
|
1 |
|
$params_ref = $request->getBodyParams(); |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
if (!empty($params)) { |
138
|
1 |
|
$params_ref->enhance($params); |
139
|
1 |
|
} |
140
|
1 |
|
$params_ref['access_token'] = $this->getSession()->getAccessToken(); |
141
|
1 |
|
$params_ref['appsecret_proof'] = $this->getSession()->getAppSecretProof(); |
142
|
|
|
|
143
|
1 |
|
return $request; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param RequestInterface $request |
148
|
|
|
* @return ResponseInterface |
149
|
|
|
*/ |
150
|
1 |
|
public function executeRequest(RequestInterface $request) { |
151
|
1 |
|
$this->getLogger()->logRequest('debug', $request); |
152
|
|
|
try { |
153
|
1 |
|
$response = $request->execute(); |
154
|
1 |
|
} catch (RequestException $e) { |
155
|
|
|
$this->getLogger()->logResponse('debug', @$e->getResponse()); |
156
|
|
|
throw $e; |
157
|
|
|
} |
158
|
1 |
|
$this->getLogger()->logResponse('debug', $response); |
159
|
|
|
|
160
|
1 |
|
return $response; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
2 |
|
public function getDefaultGraphVersion() { |
167
|
2 |
|
if ($this->defaultGraphVersion === null) { |
168
|
2 |
|
$match = array(); |
169
|
2 |
|
if (preg_match("/^\d+\.\d+/", static::VERSION, $match)) { |
170
|
2 |
|
$this->defaultGraphVersion = $match[0]; |
171
|
2 |
|
} |
172
|
2 |
|
} |
173
|
|
|
|
174
|
2 |
|
return $this->defaultGraphVersion; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param string $version |
179
|
|
|
*/ |
180
|
1 |
|
public function setDefaultGraphVersion($version) { |
181
|
1 |
|
$this->defaultGraphVersion = $version; |
182
|
1 |
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Make graph api calls |
186
|
|
|
* |
187
|
|
|
* @param string $path Ads API endpoint |
188
|
|
|
* @param string $method Ads API request type |
189
|
|
|
* @param array $params Assoc of request parameters |
190
|
|
|
* @return ResponseInterface Graph API responses |
191
|
|
|
*/ |
192
|
1 |
|
public function call( |
193
|
|
|
$path, |
194
|
|
|
$method = RequestInterface::METHOD_GET, |
195
|
|
|
array $params = array()) { |
196
|
|
|
|
197
|
1 |
|
$request = $this->prepareRequest($path, $method, $params); |
198
|
|
|
|
199
|
1 |
|
return $this->executeRequest($request); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return Session |
204
|
|
|
*/ |
205
|
3 |
|
public function getSession() { |
206
|
3 |
|
return $this->session; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param LoggerInterface $logger |
211
|
|
|
*/ |
212
|
2 |
|
public function setLogger(LoggerInterface $logger) { |
213
|
2 |
|
$this->logger = $logger; |
214
|
2 |
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return LoggerInterface |
218
|
|
|
*/ |
219
|
2 |
|
public function getLogger() { |
220
|
2 |
|
if ($this->logger === null) { |
221
|
1 |
|
$this->logger = new NullLogger(); |
222
|
1 |
|
} |
223
|
2 |
|
return $this->logger; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @return Client |
228
|
|
|
*/ |
229
|
2 |
|
public function getHttpClient() { |
230
|
2 |
|
return $this->httpClient; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|