Bicycle/System/Classes/HTTP/Client/Curl.php
Egor Isaev b516ca07dc Initial commit: Bicycle PHP MVC micro-framework
Core MVC, HTTP client, Session, Cookie, Config, HTTPException — 111 PHPUnit tests.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-04 10:04:19 +03:00

56 lines
1.4 KiB
PHP

<?php
/**
* @package Bicycle
* @author Egor Isaev
* @description HTTP/Client/Curl.php
* @copyright (c) 04/06/2026
*/
namespace System\Classes\HTTP\Client;
use RuntimeException;
class Curl
{
public function execute(Request $request): Response
{
$headers = [];
foreach ($request->getHeaders() as $name => $value) {
$headers[] = $name . ': ' . $value;
}
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $request->getUrl(),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_TIMEOUT => $request->getTimeout(),
CURLOPT_CONNECTTIMEOUT => 10,
]);
match ($request->getMethod()) {
'GET' => curl_setopt($ch, CURLOPT_HTTPGET, true),
'POST' => curl_setopt($ch, CURLOPT_POST, true),
default => curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request->getMethod()),
};
if ($request->getBody() !== null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $request->getBody());
}
$body = curl_exec($ch);
if ($body === false) {
$error = curl_error($ch);
curl_close($ch);
throw new RuntimeException('Curl error: ' . $error);
}
$status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return new Response($status, $body);
}
}