Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions src/Api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace Bannerbear;

class Api
{
/** @var \CurlHandle */
private $client;
/** @var string */
protected $url;

/**
* @param array<int,string> $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<string,mixed> $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<string,mixed> $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);
}
}
103 changes: 0 additions & 103 deletions src/BannerbearClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int,string> $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<string,mixed> $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<string,mixed> $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);
}
}
2 changes: 1 addition & 1 deletion src/example.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
require_once('src/BannerbearClient.php');
require_once(__DIR__ . '/../vendor/autoload.php');

$bb = new Bannerbear\BannerbearClient('');

Expand Down