Created
July 25, 2023 11:28
-
-
Save ewilan-riviere/8c0b95e560514e391fb6ab7c024e19ee to your computer and use it in GitHub Desktop.
PHP example to send HTTP Post with cURL, Stream or Guzzle.
This file contains 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 | |
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