diff --git a/src/Api.php b/src/Api.php new file mode 100644 index 0000000..860bf55 --- /dev/null +++ b/src/Api.php @@ -0,0 +1,105 @@ + $headers + */ + public function __construct(string $url, array $headers = []) + { + $curlClient = curl_init(); + curl_setopt_array($curlClient, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_HTTPHEADER => $headers, + CURLOPT_FAILONERROR => true, + ]); + $this->client = $curlClient; + $this->url = $url; + return $this; + } + + private function getUrl(string $url) + { + return $this->url . $url; + } + + public function get(string $url) + { + curl_setopt($this->client, CURLOPT_URL, $this->getUrl($url)); + + /** @var string $res */ + $res = curl_exec($this->client); + + if (curl_errno($this->client)) { + $error_msg = curl_error($this->client); + $status = curl_getinfo($this->client, CURLINFO_RESPONSE_CODE); + } + + curl_close($this->client); + + if (isset($error_msg)) { + throw new \Exception('Bannerbear Error Status: ' . $status . '. Message: ' . $error_msg); + } + + return json_decode($res, true); + } + + /** + * @param array $params + */ + public function patch(string $url, array $params) + { + curl_setopt($this->client, CURLOPT_URL, $this->getUrl($url)); + curl_setopt($this->client, CURLOPT_CUSTOMREQUEST, 'PATCH'); + curl_setopt($this->client, CURLOPT_POSTFIELDS, json_encode($params)); + + /** @var string $res */ + $res = curl_exec($this->client); + + if (curl_errno($this->client)) { + $error_msg = curl_error($this->client); + $status = curl_getinfo($this->client, CURLINFO_RESPONSE_CODE); + } + + curl_close($this->client); + + if (isset($error_msg)) { + throw new \Exception('Bannerbear Error Status: ' . $status . '. Message: ' . $error_msg); + } + + return json_decode($res, true); + } + + /** + * @param array $params + */ + public function post(string $url, array $params) + { + curl_setopt($this->client, CURLOPT_URL, $this->getUrl($url)); + curl_setopt($this->client, CURLOPT_POST, true); + curl_setopt($this->client, CURLOPT_POSTFIELDS, json_encode($params)); + + /** @var string $res */ + $res = curl_exec($this->client); + + if (curl_errno($this->client)) { + $error_msg = curl_error($this->client); + $status = curl_getinfo($this->client, CURLINFO_RESPONSE_CODE); + } + + curl_close($this->client); + + if (isset($error_msg)) { + throw new \Exception('Bannerbear Error Status: ' . $status . '. Message: ' . $error_msg); + } + + return json_decode($res, true); + } +} diff --git a/src/BannerbearClient.php b/src/BannerbearClient.php index 235b92f..f53b69e 100644 --- a/src/BannerbearClient.php +++ b/src/BannerbearClient.php @@ -287,106 +287,3 @@ public function generate_signed_url(string $base_id, array $params, bool $synchr return $base . $query . "&s=" . $signature; } } - - -class Api -{ - /** @var \CurlHandle */ - private $client; - /** @var string */ - protected $url; - - /** - * @param array $headers - */ - public function __construct(string $url, array $headers = []) - { - $curlClient = curl_init(); - curl_setopt_array($curlClient, [ - CURLOPT_RETURNTRANSFER => true, - CURLOPT_HTTPHEADER => $headers, - CURLOPT_FAILONERROR => true, - ]); - $this->client = $curlClient; - $this->url = $url; - return $this; - } - - private function getUrl(string $url) - { - return $this->url . $url; - } - - public function get(string $url) - { - curl_setopt($this->client, CURLOPT_URL, $this->getUrl($url)); - - /** @var string $res */ - $res = curl_exec($this->client); - - if (curl_errno($this->client)) { - $error_msg = curl_error($this->client); - $status = curl_getinfo($this->client, CURLINFO_RESPONSE_CODE); - } - - curl_close($this->client); - - if (isset($error_msg)) { - throw new \Exception('Bannerbear Error Status: ' . $status . '. Message: ' . $error_msg); - } - - return json_decode($res, true); - } - - /** - * @param array $params - */ - public function patch(string $url, array $params) - { - curl_setopt($this->client, CURLOPT_URL, $this->getUrl($url)); - curl_setopt($this->client, CURLOPT_CUSTOMREQUEST, 'PATCH'); - curl_setopt($this->client, CURLOPT_POSTFIELDS, json_encode($params)); - - /** @var string $res */ - $res = curl_exec($this->client); - - if (curl_errno($this->client)) { - $error_msg = curl_error($this->client); - $status = curl_getinfo($this->client, CURLINFO_RESPONSE_CODE); - } - - curl_close($this->client); - - if (isset($error_msg)) { - throw new \Exception('Bannerbear Error Status: ' . $status . '. Message: ' . $error_msg); - } - - return json_decode($res, true); - } - - /** - * @param array $params - */ - public function post(string $url, array $params) - { - curl_setopt($this->client, CURLOPT_URL, $this->getUrl($url)); - curl_setopt($this->client, CURLOPT_POST, true); - curl_setopt($this->client, CURLOPT_POSTFIELDS, json_encode($params)); - - /** @var string $res */ - $res = curl_exec($this->client); - - if (curl_errno($this->client)) { - $error_msg = curl_error($this->client); - $status = curl_getinfo($this->client, CURLINFO_RESPONSE_CODE); - } - - curl_close($this->client); - - if (isset($error_msg)) { - throw new \Exception('Bannerbear Error Status: ' . $status . '. Message: ' . $error_msg); - } - - return json_decode($res, true); - } -} diff --git a/src/example.php b/src/example.php index d1f5b2a..1a66ce2 100644 --- a/src/example.php +++ b/src/example.php @@ -1,5 +1,5 @@