Skip to content

Instantly share code, notes, and snippets.

@analogic
Last active March 22, 2024 21:26
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save analogic/8fcdc87ce8d4d8adfed9 to your computer and use it in GitHub Desktop.
Save analogic/8fcdc87ce8d4d8adfed9 to your computer and use it in GitHub Desktop.
<?php
$c = new Client('https://poste.io/admin/api/v1/', 'admin@poste.io', 'admin');
$c->delete('domains/t.com');
$c->post('domains', ['name' => 't.com']);
$c->post('boxes', ['email' => 't@t.com', 'passwordPlaintext' => 't', 'name' => 't']);
$c->patch('boxes/t@t.com', ['name' => 'a', 'disabled' => true, 'passwordPlaintext' => 'a']);
$box = $c->get('boxes/t@t.com');
echo $box->name === 'a' ? "OK\n" : "ERR\n";
echo $box->disabled === true ? "OK\n" : "ERR\n";
$c->patch('boxes/t@t.com', ['name' => 'b', 'disabled' => false, 'passwordPlaintext' => 'b']);
$box = $c->get('boxes/t@t.com');
echo $box->name === 'b' ? "OK\n" : "ERR\n";
echo $box->disabled === false ? "OK\n" : "ERR\n";
$c->patch('boxes/t@t.com', ['disabled' => true, 'passwordPlaintext' => 'c']);
$box = $c->get('boxes/t@t.com');
echo $box->name === 'b' ? "OK\n" : "ERR\n";
echo $box->disabled === true ? "OK\n" : "ERR\n";
print_r($box);
$c->delete('domains/t.com');
class Client {
private $base;
private $user;
private $password;
public function __construct($base, $user, $password)
{
$this->base = $base;
$this->user = $user;
$this->password = $password;
}
private function curl($method, $url, $data = null)
{
$headers = ['Accept: application/json', 'Content-Type: application/json'];
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $this->base.$url);
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_USERPWD, $this->user. ":" . $this->password);
switch ($method) {
case 'GET':
break;
case 'POST':
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($data));
break;
case 'PUT':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($data));
break;
case 'PATCH':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($data));
break;
case 'DELETE':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
}
$response = curl_exec($handle);
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if(!preg_match('~^2~', $code)) {
echo 'Got code '.$code." and response:\n".$response."\n";
return null;
} else {
return $code != 204 ? json_decode($response) : null;
}
}
public function get($url)
{
return $this->curl('GET', $url);
}
public function post($url, $data)
{
return $this->curl('POST', $url, $data);
}
public function patch($url, $data)
{
return $this->curl('PATCH', $url, $data);
}
public function delete($url)
{
return $this->curl('DELETE', $url);
}
public function put($url, $data)
{
return $this->curl('PUT', $url, $data);
}
}
@Clivkc
Copy link

Clivkc commented Nov 22, 2023

6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment