|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Coco\SourceWatcher\Core\Api; |
|
4
|
|
|
|
|
5
|
|
|
use Coco\SourceWatcher\Core\SourceWatcherException; |
|
6
|
|
|
use Coco\SourceWatcher\Utils\Internationalization; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class ApiReader |
|
10
|
|
|
* |
|
11
|
|
|
* When you describe the endpoint, you list the end path only (hence the term "end point"). |
|
12
|
|
|
* The full path that contains both the base path and the endpoint is often called a resource URL. |
|
13
|
|
|
* |
|
14
|
|
|
* @package Coco\SourceWatcher\Core\Api |
|
15
|
|
|
*/ |
|
16
|
|
|
class ApiReader implements Reader |
|
17
|
|
|
{ |
|
18
|
|
|
protected ?string $resourceURL = null; |
|
19
|
|
|
|
|
20
|
|
|
protected int $timeout = 5; |
|
21
|
|
|
|
|
22
|
|
|
protected array $headers = []; |
|
23
|
|
|
|
|
24
|
|
|
protected int $currentAttempt; |
|
25
|
|
|
|
|
26
|
|
|
protected int $attempts = 3; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct () |
|
29
|
|
|
{ |
|
30
|
|
|
$this->currentAttempt = 1; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function getResourceURL () : string |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->resourceURL; |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function setResourceURL ( string $resourceURL ) : void |
|
39
|
|
|
{ |
|
40
|
|
|
$this->resourceURL = $resourceURL; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getTimeout () : int |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->timeout; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function setTimeout ( int $timeout ) : void |
|
49
|
|
|
{ |
|
50
|
|
|
$this->timeout = $timeout; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getHeaders () : array |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->headers; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function setHeaders ( array $headers ) : void |
|
59
|
|
|
{ |
|
60
|
|
|
$this->headers = $headers; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return bool|mixed|string |
|
65
|
|
|
* @throws SourceWatcherException |
|
66
|
|
|
*/ |
|
67
|
|
|
public function read () |
|
68
|
|
|
{ |
|
69
|
|
|
if ( $this->resourceURL == null || $this->resourceURL == "" ) { |
|
|
|
|
|
|
70
|
|
|
throw new SourceWatcherException( Internationalization::getInstance()->getText( ApiReader::class, |
|
71
|
|
|
"No_Resource_URL_Found" ) ); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$curl = curl_init(); |
|
75
|
|
|
|
|
76
|
|
|
curl_setopt( $curl, CURLOPT_URL, $this->resourceURL ); |
|
77
|
|
|
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 ); |
|
78
|
|
|
curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, $this->timeout ); |
|
79
|
|
|
|
|
80
|
|
|
if ( !empty( $this->headers ) ) { |
|
81
|
|
|
curl_setopt( $curl, CURLOPT_HTTPHEADER, $this->headers ); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* https://www.php.net/manual/en/function.curl-exec.php |
|
86
|
|
|
* |
|
87
|
|
|
* Returns TRUE on success or FALSE on failure. |
|
88
|
|
|
* However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure. |
|
89
|
|
|
*/ |
|
90
|
|
|
|
|
91
|
|
|
$response = curl_exec( $curl ); |
|
92
|
|
|
|
|
93
|
|
|
curl_close( $curl ); |
|
94
|
|
|
|
|
95
|
|
|
if ( !$response && $this->currentAttempt < $this->attempts ) { |
|
96
|
|
|
$this->currentAttempt++; |
|
97
|
|
|
|
|
98
|
|
|
return $this->read(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $response; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|