Skip to content

Instantly share code, notes, and snippets.

@ewilan-riviere
Created July 25, 2023 11:28
Show Gist options
  • Save ewilan-riviere/8c0b95e560514e391fb6ab7c024e19ee to your computer and use it in GitHub Desktop.
Save ewilan-riviere/8c0b95e560514e391fb6ab7c024e19ee to your computer and use it in GitHub Desktop.
PHP example to send HTTP Post with cURL, Stream or Guzzle.
<?php
namespace Kiwilan\Sentinel;
use Exception;
class ClientPost
{
protected function __construct(
readonly protected string $host,
readonly protected string $method = 'stream', // curl, stream, guzzle
protected array $payload = [],
protected int $status = 0,
protected ?string $message = null,
) {
}
public static function make(string $host, array $payload = [], string $method = 'stream'): self
{
$self = new self(
host: $host,
method: $method,
payload: $payload,
);
return $self;
}
public function execute(): array
{
if ($this->method === 'curl') {
return $this->curl();
}
if ($this->method === 'stream') {
return $this->stream();
}
return $this->guzzle();
}
private function guzzle(): array
{
$client = new \GuzzleHttp\Client();
$response = $client->post($this->host, [
'headers' => [
'Content-type' => 'application/json',
'Accept' => 'application/json',
],
'json' => $this->payload,
]);
$this->status = $response->getStatusCode();
$body = $response->getBody()->getContents();
$this->message = $body;
if ($this->status !== 201 && $this->status !== 200 && $this->status !== 0) {
throw new Exception("Error {$this->status}: {$this->message}");
}
return [
'headers' => $response->getHeaders(),
'status' => $this->status,
'body' => $body,
'json' => json_decode($body, true),
];
}
private function stream(): array
{
$options = [
'http' => [
'header' => "Content-Type: application/json\r\n".
"Accept: application/json\r\n",
'method' => 'POST',
'content' => json_encode($this->payload),
],
];
$context = stream_context_create($options);
$res = file_get_contents($this->host, false, $context);
$status = $http_response_header[0];
$this->status = (int) substr($status, 9, 3);
$this->message = $res;
if ($res === false) {
throw new Exception("Error {$this->status}: {$this->message}");
}
return [
'headers' => [],
'status' => $this->status,
'body' => $res,
'json' => json_decode($res, true),
];
}
private function curl(): array
{
$content = json_encode($this->payload);
$curl = curl_init($this->host);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'Content-type: application/json',
'Accept: application/json',
]);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
$json = curl_exec($curl);
$this->status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$headers = curl_getinfo($curl, CURLINFO_HEADER_OUT);
$message = json_decode($json, true);
$this->message = $message['message'] ?? $json;
if ($this->status !== 201 && $this->status !== 200 && $this->status !== 0) {
throw new Exception("Error {$this->status}: {$this->message}");
}
curl_close($curl);
$json = json_decode($json, true);
return [
'headers' => $headers,
'status' => $this->status,
'message' => $this->message,
'json' => $json,
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment