1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Coco\SourceWatcher\Core\Api; |
4
|
|
|
|
5
|
|
|
use Coco\SourceWatcher\Core\SourceWatcherException; |
6
|
|
|
use Coco\SourceWatcher\Utils\i18n; |
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
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected ?string $resourceURL = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
protected int $timeout = 5; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected array $headers = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
protected int $currentAttempt; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
protected int $attempts = 3; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* ApiReader constructor. |
45
|
|
|
*/ |
46
|
|
|
public function __construct () |
47
|
|
|
{ |
48
|
|
|
$this->currentAttempt = 1; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function getResourceURL () : string |
55
|
|
|
{ |
56
|
|
|
return $this->resourceURL; |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $resourceURL |
61
|
|
|
*/ |
62
|
|
|
public function setResourceURL ( string $resourceURL ) : void |
63
|
|
|
{ |
64
|
|
|
$this->resourceURL = $resourceURL; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return int |
69
|
|
|
*/ |
70
|
|
|
public function getTimeout () : int |
71
|
|
|
{ |
72
|
|
|
return $this->timeout; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param int $timeout |
77
|
|
|
*/ |
78
|
|
|
public function setTimeout ( int $timeout ) : void |
79
|
|
|
{ |
80
|
|
|
$this->timeout = $timeout; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
public function getHeaders () : array |
87
|
|
|
{ |
88
|
|
|
return $this->headers; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param array $headers |
93
|
|
|
*/ |
94
|
|
|
public function setHeaders ( array $headers ) : void |
95
|
|
|
{ |
96
|
|
|
$this->headers = $headers; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return bool|string |
101
|
|
|
* @throws SourceWatcherException |
102
|
|
|
*/ |
103
|
|
|
public function read () |
104
|
|
|
{ |
105
|
|
|
if ( $this->resourceURL == null || $this->resourceURL == "" ) { |
|
|
|
|
106
|
|
|
throw new SourceWatcherException( i18n::getInstance()->getText( "en_US", ApiReader::class, "No_Resource_URL_Found" ) ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$curl = curl_init(); |
110
|
|
|
|
111
|
|
|
curl_setopt( $curl, CURLOPT_URL, $this->resourceURL ); |
|
|
|
|
112
|
|
|
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 ); |
113
|
|
|
curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, $this->timeout ); |
114
|
|
|
|
115
|
|
|
if ( !empty( $this->headers ) ) { |
116
|
|
|
curl_setopt( $curl, CURLOPT_HTTPHEADER, $this->headers ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* https://www.php.net/manual/en/function.curl-exec.php |
121
|
|
|
* |
122
|
|
|
* Returns TRUE on success or FALSE on failure. |
123
|
|
|
* However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure. |
124
|
|
|
*/ |
125
|
|
|
|
126
|
|
|
$response = curl_exec( $curl ); |
|
|
|
|
127
|
|
|
|
128
|
|
|
curl_close( $curl ); |
|
|
|
|
129
|
|
|
|
130
|
|
|
if ( $response == false ) { |
|
|
|
|
131
|
|
|
if ( $this->currentAttempt < $this->attempts ) { |
132
|
|
|
$this->currentAttempt++; |
133
|
|
|
$this->read(); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $response; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|