chore: lots of changes
This commit is contained in:
49
src/Framework/HttpClient/CurlHttpClient.php
Normal file
49
src/Framework/HttpClient/CurlHttpClient.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Framework\HttpClient;
|
||||
|
||||
use App\Framework\Http\Headers;
|
||||
use App\Framework\Http\Status;
|
||||
|
||||
final readonly class CurlHttpClient implements HttpClient
|
||||
{
|
||||
|
||||
public function send(ClientRequest $request): ClientResponse
|
||||
{
|
||||
$ch = curl_init();
|
||||
|
||||
$options = [
|
||||
CURLOPT_URL => $request->url,
|
||||
CURLOPT_CUSTOMREQUEST => $request->method->value,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_HEADER => true,
|
||||
];
|
||||
if ($request->body !== '') {
|
||||
$options[CURLOPT_POSTFIELDS] = $request->body;
|
||||
}
|
||||
if (count($request->headers->all()) > 0) {
|
||||
$options[CURLOPT_HTTPHEADER] = $request->headers->all();
|
||||
}
|
||||
|
||||
curl_setopt_array($ch, $options);
|
||||
|
||||
$raw = curl_exec($ch);
|
||||
|
||||
if ($raw === false) {
|
||||
throw new HttpException(curl_error($ch), curl_errno($ch));
|
||||
}
|
||||
|
||||
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
|
||||
$headersRaw = substr($raw, 0, $headerSize);
|
||||
$body = substr($raw, $headerSize);
|
||||
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
|
||||
curl_close($ch);
|
||||
|
||||
#$headers = Headers::fromString($headersRaw);
|
||||
$headers = new Headers();
|
||||
|
||||
return new ClientResponse(Status::from($status), $headers, $body);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user