OOP Curl API in PHP 8.4
Marcos Marcolin • February 24, 2024 • 1 min read
php php84 phprfc opensourceObject-Oriented API for the cURL extension
PHP will get an OOP API for the cURL extension in version 8.4.0.
That's what the RFC proposes: Add OOP methods to Curl objects.
The proposal is part of the ongoing package of converting Resources into Objects in PHP, which has been underway for a while now.
The proposal introduces an object-oriented implementation for the cURL extension, used to make web requests over several protocols, such as HTTP and HTTPS.
In short, aliases will be created for several curl_* functions, such as curl_init(), curl_exec(), curl_setopt(), etc. This will also cover multi_curl_*() and curl_share_*().
At the moment, it's under discussion, and it will most likely be approved once it goes to a vote.
Personally, this will be an excellent addition to the language's ongoing modernization.
Below are a few sections of the proposed API.
CurlHandle
/**
* @strict-properties
* @not-serializable
*/
final class CurlHandle
{
/**
* Behaves similarly to `curl_init(?string $uri = null): \CurlHandle`,
* however this constructor implicitly sets CURLOPT_RETURNTRANSFER == true.
*/
public function __construct(?string $uri = null) {}
/** @alias curl_errno */
public function errno(): int {}
/** @alias curl_error */
public function error(): string {}
/** @alias curl_strerror */
static public function strerror(int $code): string {}
/** @alias curl_escape */
public function escape(string $str): string {}
/** @alias curl_unescape */
public function unescape(string $str): string {}
/**
* @throws CurlHandleException
*
* Similar to `curl_exec(\CurlHandle $ch): string|bool`,
* but never returns False, since errors are promoted to exceptions.
*/
public function exec(): string|true {}
/** @alias curl_pause */
public function pause(int $flags): int {}
/** @alias curl_reset */
public function reset(): void {}
/** @alias curl_getinfo */
public function getInfo(?int $option = null): mixed {}
#if LIBCURL_VERSION_NUM >= 0x073E00 /* Available since 7.62.0 */
/** @alias curl_upkeep */
public function upkeep(): bool {}
#endif
/**
* Varies from curl_setopt() to allow fluent chaining.
* @throws CurlHandleException
*/
public function setOpt(int $option, mixed $value): \CurlHandle {}
/**
* Returns self to match setOpt() behavior.
* @throws CurlHandleException
*/
public function setOptArray(array $option): \CurlHandle {}
}
CurlMultiHandle
/**
* @strict-properties
* @not-serializable
*/
final class CurlMultiHandle
{
/**
* Returns self for fluent calling.
* @throws CurlException.
*/
public function addHandle(\CurlHandle $handle): \CurlMultiHandle {}
/**
* Returns self for fluent calling.
* @throws CurlException.
*/
public function removeHandle(\CurlHandle $handle): \CurlMultiHandle {}
/**
* Returns self for fluent calling.
* @throws CurlException.
*/
public function setOpt(int $option, mixed $value): \CurlMultiHandle {}
/** @alias curl_multi_errno */
public function errno(): int {}
/** @alias curl_multi_error */
public function error(): ?string {}
/** @alias curl_multi_strerror */
public function strerror(int $error_code): ?string {}
/**
* Returns TRUE if still running, FALSE otherwise.
* @param int $still_running
* @throws CurlException.
*/
public function exec(&$still_running): bool {}
/** @alias curl_multi_getcontent */
static public function getContent(\CurlHandle $handle): ?string {}
/**
* @alias curl_multi_info_read
* @param int $queued_messages
* @return array<string, int|object>|false
* @refcount 1
*/
public function infoRead(&$queued_messages = null): array|false {}
/** @alias curl_multi_select */
public function select(float $timeout = 1.0): int {}
}
CurlMultiHandle
/**
* @strict-properties
* @not-serializable
*/
final class CurlShareHandle
{
/** @alias curl_share_errno */
public function errno(): int {}
/** @alias curl_share_error */
public function error(): ?string {}
/** @alias curl_share_strerror */
static public function strerror(): ?string {}
/**
* Returns self for fluent calling.
* @throws CurlException.
*/
public function setOpt(int $option, mixed $value): \CurlShareHandle {}
}
You can read more about the proposal in the official post: click here.