This repository has been archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 148
/
Client.php
302 lines (262 loc) · 10.1 KB
/
Client.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/
namespace Facebook\InstantArticles\Client;
use Facebook\Exceptions\FacebookSDKException;
use Facebook\Facebook;
use Facebook\InstantArticles\Elements\InstantArticle;
use Facebook\InstantArticles\Elements\InstantArticleInterface;
use Facebook\InstantArticles\Validators\Type;
class Client
{
const EDGE_NAME = '/instant_articles';
/**
* @var Facebook The main Facebook service client.
*/
private $facebook;
/**
* @var int ID of the Facebook Page we are using for Instant Articles
*/
protected $pageID;
/**
* @var bool|false Are we using the Instant Articles development sandbox?
*/
protected $developmentMode = false;
/**
* @param Facebook $facebook the main Facebook service client
* @param string $pageID Specify the Facebook Page to use for Instant Articles
* @param bool $developmentMode|false Configure the service to use the Instant Articles development sandbox
*/
public function __construct($facebook, $pageID, $developmentMode = false)
{
Type::enforce($facebook, 'Facebook\Facebook');
Type::enforce($pageID, Type::STRING);
Type::enforce($developmentMode, Type::BOOLEAN);
// TODO throw if $facebook doesn't have a default_access_token
$this->facebook = $facebook;
$this->pageID = $pageID;
$this->developmentMode = $developmentMode;
}
/**
* Creates a client with a proper Facebook client instance.
*
* @param string $appID
* @param string $appSecret
* @param string $accessToken The page access token used to query the Facebook Graph API
* @param string $pageID Specify the Facebook Page to use for Instant Articles
* @param bool $developmentMode|false Configure the service to use the Instant Articles development sandbox
*
* @return static
*
* @throws FacebookSDKException
*/
public static function create($appID, $appSecret, $accessToken, $pageID, $developmentMode = false)
{
Type::enforce($appID, Type::STRING);
Type::enforce($appSecret, Type::STRING);
Type::enforce($accessToken, Type::STRING);
$facebook = new Facebook([
'app_id' => $appID,
'app_secret' => $appSecret,
'default_access_token' => $accessToken,
'default_graph_version' => 'v2.5'
]);
return new static($facebook, $pageID, $developmentMode);
}
/**
* Import an article into your Instant Articles library.
*
* @param InstantArticle $article The article to import
* @param bool|false $published Specifies if this article should be taken live or not. Optional. Default: false.
* @param bool|false $formatOutput Specifies if this article should be formatted after transformation. Optional. Default: false.
*
* @return int The submission status ID. It is not the article ID. (Since 1.3.0)
*/
public function importArticle($article, $published = false, $forceRescrape = false, $formatOutput = false)
{
Type::enforce($article, 'Facebook\InstantArticles\Elements\InstantArticleInterface');
Type::enforce($published, Type::BOOLEAN);
// Never try to take live if we're in development (the API would throw an error if we tried)
$published = $this->developmentMode ? false : $published;
// Assume default access token is set on $this->facebook
$response = $this->facebook->post($this->pageID . Client::EDGE_NAME, [
'html_source' => $article->render(null, $formatOutput),
'published' => $published,
'development_mode' => $this->developmentMode,
]);
if ($forceRescrape) {
// Re-scrape Graph object for article URL
$this->scrapeArticleURL($article->getCanonicalURL());
}
return $response->getGraphNode()->getField('id');
}
/**
* Scrape Graph object for given URL
*
* @param string $canonicalURL The URL that will be scraped.
*/
private function scrapeArticleURL($canonicalURL)
{
$this->facebook->post('/', [
'id' => $canonicalURL,
'scrape' => 'true',
]);
}
/**
* Removes an article from your Instant Articles library.
*
* @param string $canonicalURL The canonical URL of the article to delete.
*
* @return InstantArticleStatus
*
* @todo Consider returning the \Facebook\FacebookResponse object sent by
* \Facebook\Facebook::delete(). For now we trust that if an Instant
* Article ID exists for the Canonical URL the delete operation will work.
*/
public function removeArticle($canonicalURL)
{
if (!$canonicalURL) {
return InstantArticleStatus::notFound(['$canonicalURL param not passed to ' . __FUNCTION__ . '.']);
}
Type::enforce($canonicalURL, Type::STRING);
if ($articleID = $this->getArticleIDFromCanonicalURL($canonicalURL)) {
$this->facebook->delete($articleID);
return InstantArticleStatus::success();
}
return InstantArticleStatus::notFound([ServerMessage::info('An Instant Article ID ' . $articleID . ' was not found for ' . $canonicalURL . ' in ' . __FUNCTION__ . '.')]);
}
/**
* Get an Instant Article ID on its canonical URL.
*
* @param string $canonicalURL The canonical URL of the article to get the status for.
* @return int|null the article ID or null if not found
*/
public function getArticleIDFromCanonicalURL($canonicalURL)
{
Type::enforce($canonicalURL, Type::STRING);
$field = $this->developmentMode ? 'development_instant_article' : 'instant_article';
$response = $this->facebook->get('?id=' . $canonicalURL . '&fields=' . $field);
$instantArticle = $response->getGraphNode()->getField($field);
if (!$instantArticle) {
return null;
}
$articleID = $instantArticle->getField('id');
return $articleID;
}
/**
* Get the last submission status of an Instant Article.
*
* @param string|null $articleID the article ID
* @return InstantArticleStatus
*/
public function getLastSubmissionStatus($articleID)
{
if (!$articleID) {
return InstantArticleStatus::notFound();
}
Type::enforce($articleID, Type::STRING);
// Get the latest import status of this article
$response = $this->facebook->get($articleID . '?fields=most_recent_import_status');
$articleStatus = $response->getGraphNode()->getField('most_recent_import_status');
$messages = [];
if (isset($articleStatus['errors'])) {
foreach ($articleStatus['errors'] as $error) {
$messages[] = ServerMessage::fromLevel($error['level'], $error['message']);
}
}
return InstantArticleStatus::fromStatus($articleStatus['status'], $messages);
}
/**
* Get the submission status of an Instant Article.
*
* @param string|null $submissionStatusID the submission status ID
* @return InstantArticleStatus
*/
public function getSubmissionStatus($submissionStatusID)
{
if (!$submissionStatusID) {
return InstantArticleStatus::notFound();
}
Type::enforce($submissionStatusID, Type::STRING);
$response = $this->facebook->get($submissionStatusID . '?fields=status,errors');
$articleStatus = $response->getGraphNode();
$messages = [];
$errors = $articleStatus->getField('errors');
if (null !== $errors) {
foreach ($errors as $error) {
$messages[] = ServerMessage::fromLevel($error['level'], $error['message']);
}
}
return InstantArticleStatus::fromStatus($articleStatus->getField('status'), $messages);
}
/**
* Get the review submission status
*
* @return string The review status
*/
public function getReviewSubmissionStatus()
{
$response = $this->facebook->get('me?fields=instant_articles_review_status');
return $response->getGraphNode()->getField('instant_articles_review_status');
}
/**
* Retrieve the article URLs already published on Instant Articles
*
* @return string[] The cannonical URLs from articles
*/
public function getArticlesURLs($limit = 10, $development_mode = false)
{
$articleURLs = [];
$response = $this->facebook->get(
'me/instant_articles?fields=canonical_url&'.
'development_mode='.($development_mode ? 'true' : 'false').
'&limit='.$limit
);
$articles = $response->getGraphEdge();
foreach ($articles as $article) {
$articleURLs[] = $article['canonical_url'];
}
return $articleURLs;
}
/**
* Claims an URL for the page
*
* @param string $url The root URL of the site
*/
public function claimURL($url)
{
// Remove protocol from the URL
$url = preg_replace('/^https?:\/\//i', '', $url);
$response = $this->facebook->post($this->pageID . '/claimed_urls?url=' . urlencode($url));
$node = $response->getGraphNode();
$error = $node->getField('error');
$success = $node->getField('success');
if ($error) {
throw new ClientException($error['error_user_msg']);
}
if (!$success) {
throw new ClientException('Could not claim the URL');
}
}
/**
* Submits the page for review
*/
public function submitForReview()
{
$response = $this->facebook->post($this->pageID . '/?instant_articles_submit_for_review=true');
$node = $response->getGraphNode();
$error = $node->getField('error');
$success = $node->getField('success');
if ($error) {
throw new ClientException($error['error_user_msg']);
}
if (!$success) {
throw new ClientException('Could not submit for review');
}
}
}