1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Swader\Diffbot;
|
4
|
|
|
|
5
|
|
|
use Http\Discovery\HttpClientDiscovery;
|
6
|
|
|
use Http\Discovery\MessageFactoryDiscovery;
|
7
|
|
|
use Swader\Diffbot\Api\Crawl;
|
8
|
|
|
use Swader\Diffbot\Api\Custom;
|
9
|
|
|
use Swader\Diffbot\Api\Search;
|
10
|
|
|
use Swader\Diffbot\Exceptions\DiffbotException;
|
11
|
|
|
use Swader\Diffbot\Api\Product;
|
12
|
|
|
use Swader\Diffbot\Api\Image;
|
13
|
|
|
use Swader\Diffbot\Api\Analyze;
|
14
|
|
|
use Swader\Diffbot\Api\Article;
|
15
|
|
|
use Swader\Diffbot\Api\Discussion;
|
16
|
|
|
use Http\Client\Common\HttpMethodsClient as Client;
|
17
|
|
|
use Swader\Diffbot\Factory\Entity;
|
18
|
|
|
use Swader\Diffbot\Interfaces\Api;
|
19
|
|
|
use Swader\Diffbot\Interfaces\EntityFactory;
|
20
|
|
|
|
21
|
|
|
/**
|
22
|
|
|
* Class Diffbot
|
23
|
|
|
*
|
24
|
|
|
* The main class for API consumption
|
25
|
|
|
*
|
26
|
|
|
* @package Swader\Diffbot
|
27
|
|
|
*/
|
28
|
|
|
class Diffbot
|
29
|
|
|
{
|
30
|
|
|
/** @var string The API access token */
|
31
|
|
|
protected static $token = null;
|
32
|
|
|
|
33
|
|
|
/** @var string The instance token, settable once per new instance */
|
34
|
|
|
protected $instanceToken;
|
35
|
|
|
|
36
|
|
|
/** @var Client The HTTP clients to perform requests with */
|
37
|
|
|
protected $client;
|
38
|
|
|
|
39
|
|
|
/** @var EntityFactory The Factory which created Entities from Responses */
|
40
|
|
|
protected $factory;
|
41
|
|
|
|
42
|
|
|
/**
|
43
|
|
|
* @param string|null $token The API access token, as obtained on diffbot.com/dev
|
44
|
|
|
* @throws DiffbotException When no token is provided
|
45
|
|
|
*/
|
46
|
131 |
|
public function __construct($token = null)
|
47
|
|
|
{
|
48
|
131 |
|
if ($token === null) {
|
49
|
6 |
|
if (self::$token === null) {
|
50
|
1 |
|
$msg = 'No token provided, and none is globally set. ';
|
51
|
1 |
|
$msg .= 'Use Diffbot::setToken, or instantiate the Diffbot class with a $token parameter.';
|
52
|
1 |
|
throw new DiffbotException($msg);
|
53
|
|
|
}
|
54
|
5 |
|
} else {
|
55
|
126 |
|
self::validateToken($token);
|
56
|
126 |
|
$this->instanceToken = $token;
|
57
|
|
|
}
|
58
|
130 |
|
}
|
59
|
|
|
|
60
|
|
|
/**
|
61
|
|
|
* Sets the token for all future new instances
|
62
|
|
|
* @param string $token The API access token, as obtained on diffbot.com/dev
|
63
|
|
|
* @return void
|
64
|
|
|
*/
|
65
|
14 |
|
public static function setToken($token)
|
66
|
|
|
{
|
67
|
14 |
|
self::validateToken($token);
|
68
|
5 |
|
self::$token = $token;
|
69
|
5 |
|
}
|
70
|
|
|
|
71
|
139 |
|
private static function validateToken($token)
|
72
|
|
|
{
|
73
|
139 |
|
if (!is_string($token)) {
|
74
|
5 |
|
throw new \InvalidArgumentException('Token is not a string.');
|
75
|
|
|
}
|
76
|
134 |
|
if (strlen($token) < 4) {
|
77
|
4 |
|
throw new \InvalidArgumentException('Token "' . $token . '" is too short, and thus invalid.');
|
78
|
|
|
}
|
79
|
130 |
|
return true;
|
80
|
|
|
}
|
81
|
|
|
|
82
|
|
|
/**
|
83
|
|
|
* Returns the token that has been defined.
|
84
|
|
|
* @return null|string
|
85
|
|
|
*/
|
86
|
92 |
|
public function getToken()
|
87
|
|
|
{
|
88
|
92 |
|
return ($this->instanceToken) ? $this->instanceToken : self::$token;
|
89
|
|
|
}
|
90
|
|
|
|
91
|
|
|
/**
|
92
|
|
|
* Sets the client to be used for querying the API endpoints
|
93
|
|
|
*
|
94
|
|
|
* @param Client $client
|
95
|
|
|
* @see http://php-http.readthedocs.org/en/latest/utils/#httpmethodsclient
|
96
|
|
|
* @return $this
|
97
|
|
|
*/
|
98
|
124 |
|
public function setHttpClient(Client $client = null)
|
99
|
|
|
{
|
100
|
124 |
|
if ($client === null) {
|
101
|
9 |
|
$client = new Client(
|
102
|
9 |
|
HttpClientDiscovery::find(),
|
103
|
9 |
|
MessageFactoryDiscovery::find()
|
104
|
9 |
|
);
|
105
|
9 |
|
}
|
106
|
124 |
|
$this->client = $client;
|
107
|
124 |
|
return $this;
|
108
|
|
|
}
|
109
|
|
|
|
110
|
|
|
/**
|
111
|
|
|
* Returns either the instance of the Guzzle client that has been defined, or null
|
112
|
|
|
* @return Client|null
|
113
|
|
|
*/
|
114
|
123 |
|
public function getHttpClient()
|
115
|
|
|
{
|
116
|
123 |
|
return $this->client;
|
117
|
|
|
}
|
118
|
|
|
|
119
|
|
|
/**
|
120
|
|
|
* Sets the Entity Factory which will create the Entities from Responses
|
121
|
|
|
* @param EntityFactory $factory
|
122
|
|
|
* @return $this
|
123
|
|
|
*/
|
124
|
123 |
|
public function setEntityFactory(EntityFactory $factory = null)
|
125
|
|
|
{
|
126
|
123 |
|
if ($factory === null) {
|
127
|
123 |
|
$factory = new Entity();
|
128
|
123 |
|
}
|
129
|
123 |
|
$this->factory = $factory;
|
130
|
123 |
|
return $this;
|
131
|
|
|
}
|
132
|
|
|
|
133
|
|
|
/**
|
134
|
|
|
* Returns the Factory responsible for creating Entities from Responses
|
135
|
|
|
* @return EntityFactory
|
136
|
|
|
*/
|
137
|
123 |
|
public function getEntityFactory()
|
138
|
|
|
{
|
139
|
123 |
|
return $this->factory;
|
140
|
|
|
}
|
141
|
|
|
|
142
|
|
|
|
143
|
|
|
/**
|
144
|
|
|
* Creates a Product API interface
|
145
|
|
|
*
|
146
|
|
|
* @param string $url Url to analyze
|
147
|
|
|
* @return Product
|
148
|
|
|
*/
|
149
|
5 |
|
public function createProductAPI($url)
|
150
|
|
|
{
|
151
|
5 |
|
$api = new Product($url);
|
152
|
5 |
|
if (!$this->getHttpClient()) {
|
153
|
1 |
|
$this->setHttpClient();
|
154
|
1 |
|
}
|
155
|
5 |
|
if (!$this->getEntityFactory()) {
|
156
|
1 |
|
$this->setEntityFactory();
|
157
|
1 |
|
}
|
158
|
5 |
|
return $api->registerDiffbot($this);
|
159
|
|
|
}
|
160
|
|
|
|
161
|
|
|
/**
|
162
|
|
|
* Creates an Article API interface
|
163
|
|
|
*
|
164
|
|
|
* @param string $url Url to analyze
|
165
|
|
|
* @return Article
|
166
|
|
|
*/
|
167
|
10 |
|
public function createArticleAPI($url)
|
168
|
|
|
{
|
169
|
10 |
|
$api = new Article($url);
|
170
|
10 |
|
if (!$this->getHttpClient()) {
|
171
|
2 |
|
$this->setHttpClient();
|
172
|
2 |
|
}
|
173
|
10 |
|
if (!$this->getEntityFactory()) {
|
174
|
2 |
|
$this->setEntityFactory();
|
175
|
2 |
|
}
|
176
|
10 |
|
return $api->registerDiffbot($this);
|
177
|
|
|
}
|
178
|
|
|
|
179
|
|
|
/**
|
180
|
|
|
* Creates an Image API interface
|
181
|
|
|
*
|
182
|
|
|
* @param string $url Url to analyze
|
183
|
|
|
* @return Image
|
184
|
|
|
*/
|
185
|
6 |
|
public function createImageAPI($url)
|
186
|
|
|
{
|
187
|
6 |
|
$api = new Image($url);
|
188
|
6 |
|
if (!$this->getHttpClient()) {
|
189
|
1 |
|
$this->setHttpClient();
|
190
|
1 |
|
}
|
191
|
6 |
|
if (!$this->getEntityFactory()) {
|
192
|
1 |
|
$this->setEntityFactory();
|
193
|
1 |
|
}
|
194
|
6 |
|
return $api->registerDiffbot($this);
|
195
|
|
|
}
|
196
|
|
|
|
197
|
|
|
/**
|
198
|
|
|
* Creates an Analyze API interface
|
199
|
|
|
*
|
200
|
|
|
* @param string $url Url to analyze
|
201
|
|
|
* @return Analyze
|
202
|
|
|
*/
|
203
|
57 |
|
public function createAnalyzeAPI($url)
|
204
|
|
|
{
|
205
|
57 |
|
$api = new Analyze($url);
|
206
|
57 |
|
if (!$this->getHttpClient()) {
|
207
|
1 |
|
$this->setHttpClient();
|
208
|
1 |
|
}
|
209
|
57 |
|
if (!$this->getEntityFactory()) {
|
210
|
1 |
|
$this->setEntityFactory();
|
211
|
1 |
|
}
|
212
|
57 |
|
return $api->registerDiffbot($this);
|
213
|
|
|
}
|
214
|
|
|
|
215
|
|
|
/**
|
216
|
|
|
* Creates an Discussion API interface
|
217
|
|
|
*
|
218
|
|
|
* @param string $url Url to analyze
|
219
|
|
|
* @return Discussion
|
220
|
|
|
*/
|
221
|
8 |
|
public function createDiscussionAPI($url)
|
222
|
|
|
{
|
223
|
8 |
|
$api = new Discussion($url);
|
224
|
8 |
|
if (!$this->getHttpClient()) {
|
225
|
1 |
|
$this->setHttpClient();
|
226
|
1 |
|
}
|
227
|
8 |
|
if (!$this->getEntityFactory()) {
|
228
|
1 |
|
$this->setEntityFactory();
|
229
|
1 |
|
}
|
230
|
8 |
|
return $api->registerDiffbot($this);
|
231
|
|
|
}
|
232
|
|
|
|
233
|
|
|
/**
|
234
|
|
|
* Creates a generic Custom API
|
235
|
|
|
*
|
236
|
|
|
* Does not have predefined Entity, so by default returns Wildcards
|
237
|
|
|
*
|
238
|
|
|
* @param string $url Url to analyze
|
239
|
|
|
* @param string $name Name of the custom API, required to finalize URL
|
240
|
|
|
* @return Custom
|
241
|
|
|
*/
|
242
|
6 |
|
public function createCustomAPI($url, $name)
|
243
|
|
|
{
|
244
|
6 |
|
$api = new Custom($url, $name);
|
245
|
6 |
|
if (!$this->getHttpClient()) {
|
246
|
1 |
|
$this->setHttpClient();
|
247
|
1 |
|
}
|
248
|
6 |
|
if (!$this->getEntityFactory()) {
|
249
|
1 |
|
$this->setEntityFactory();
|
250
|
1 |
|
}
|
251
|
6 |
|
return $api->registerDiffbot($this);
|
252
|
|
|
}
|
253
|
|
|
|
254
|
|
|
/**
|
255
|
|
|
* Creates a new Crawljob with the given name.
|
256
|
|
|
*
|
257
|
|
|
* @see https://www.diffbot.com/dev/docs/crawl/
|
258
|
|
|
*
|
259
|
|
|
* @param string $name Name of the crawljob. Needs to be unique.
|
260
|
|
|
* @param Api $api Optional instance of an API - if omitted, must be set
|
261
|
|
|
* later manually
|
262
|
|
|
* @return Crawl
|
263
|
|
|
*/
|
264
|
61 |
|
public function crawl($name = null, Api $api = null)
|
265
|
|
|
{
|
266
|
61 |
|
$api = new Crawl($name, $api);
|
267
|
61 |
|
if (!$this->getHttpClient()) {
|
268
|
1 |
|
$this->setHttpClient();
|
269
|
1 |
|
}
|
270
|
61 |
|
if (!$this->getEntityFactory()) {
|
271
|
1 |
|
$this->setEntityFactory();
|
272
|
1 |
|
}
|
273
|
61 |
|
return $api->registerDiffbot($this);
|
274
|
|
|
}
|
275
|
|
|
|
276
|
|
|
/**
|
277
|
|
|
* Search query.
|
278
|
|
|
* @see https://www.diffbot.com/dev/docs/search/#query
|
279
|
|
|
* @param string $q
|
280
|
|
|
* @return Search
|
281
|
|
|
*/
|
282
|
14 |
|
public function search($q)
|
283
|
|
|
{
|
284
|
14 |
|
$api = new Search($q);
|
285
|
14 |
|
if (!$this->getHttpClient()) {
|
286
|
1 |
|
$this->setHttpClient();
|
287
|
1 |
|
}
|
288
|
14 |
|
if (!$this->getEntityFactory()) {
|
289
|
1 |
|
$this->setEntityFactory();
|
290
|
1 |
|
}
|
291
|
14 |
|
return $api->registerDiffbot($this);
|
292
|
|
|
}
|
293
|
|
|
} |