1 | <?php |
||
12 | abstract class Api implements \Swader\Diffbot\Interfaces\Api |
||
13 | { |
||
14 | /** @var int Timeout value in ms - defaults to 30s if empty */ |
||
15 | private $timeout = 30000; |
||
16 | |||
17 | /** @var string The URL onto which to unleash the API in question */ |
||
18 | private $url; |
||
19 | |||
20 | /** @var string API URL to which to send the request */ |
||
21 | protected $apiUrl; |
||
22 | |||
23 | /** @var array Settings on which optional fields to fetch */ |
||
24 | protected $fieldSettings = []; |
||
25 | |||
26 | /** @var array Other API specific options */ |
||
27 | protected $otherOptions = []; |
||
28 | |||
29 | /** @var Diffbot The parent class which spawned this one */ |
||
30 | protected $diffbot; |
||
31 | |||
32 | use DiffbotAware; |
||
33 | |||
34 | 122 | public function __construct($url) |
|
35 | { |
||
36 | 122 | if (strcmp($url, 'crawl') !== 0) { |
|
37 | 78 | $url = trim((string)$url); |
|
38 | 78 | if (strlen($url) < 4) { |
|
39 | 3 | throw new \InvalidArgumentException( |
|
40 | 'URL must be a string of at least four characters in length' |
||
41 | 3 | ); |
|
42 | } |
||
43 | |||
44 | 75 | $url = (isset(parse_url($url)['scheme'])) ? $url : "http://$url"; |
|
45 | |||
46 | 75 | $filtered_url = filter_var($url, FILTER_VALIDATE_URL); |
|
47 | 75 | if (!$filtered_url) { |
|
48 | throw new \InvalidArgumentException( |
||
49 | 'You provided an invalid URL: ' . $url |
||
50 | ); |
||
51 | } |
||
52 | 75 | $url = $filtered_url; |
|
53 | 75 | } |
|
54 | 119 | $this->url = $url; |
|
55 | 119 | } |
|
56 | |||
57 | /** |
||
58 | * Setting the timeout will define how long Diffbot will keep trying |
||
59 | * to fetch the API results. A timeout can happen for various reasons, from |
||
60 | * Diffbot's failure, to the site being crawled being exceptionally slow, |
||
61 | * and more. |
||
62 | * |
||
63 | * @param int|null $timeout Defaults to 30000 even if not set |
||
64 | * |
||
65 | * @return $this |
||
66 | */ |
||
67 | 12 | public function setTimeout($timeout = null) |
|
85 | |||
86 | 7 | public function call() |
|
95 | |||
96 | 79 | public function buildUrl() |
|
128 | } |
||
129 |