-
-
Save hama/1b2355817e689c93825d6bca34178fa7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$c = new Client('https://poste.io/admin/api/v1/', '[email protected]', 'admin'); | |
$c->delete('domains/t.com'); | |
$c->post('domains', ['name' => 't.com']); | |
$c->post('boxes', ['email' => '[email protected]', 'passwordPlaintext' => 't', 'name' => 't']); | |
$c->patch('boxes/[email protected]', ['name' => 'a', 'disabled' => true, 'passwordPlaintext' => 'a']); | |
$box = $c->get('boxes/[email protected]'); | |
echo $box->name === 'a' ? "OK\n" : "ERR\n"; | |
echo $box->disabled === true ? "OK\n" : "ERR\n"; | |
$c->patch('boxes/[email protected]', ['name' => 'b', 'disabled' => false, 'passwordPlaintext' => 'b']); | |
$box = $c->get('boxes/[email protected]'); | |
echo $box->name === 'b' ? "OK\n" : "ERR\n"; | |
echo $box->disabled === false ? "OK\n" : "ERR\n"; | |
$c->patch('boxes/[email protected]', ['disabled' => true, 'passwordPlaintext' => 'c']); | |
$box = $c->get('boxes/[email protected]'); | |
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment