⚠️ DEPRECATED — Useinitphp/httpinsteadAs part of the InitPHP package consolidation, this package has been retired in favor of
initphp/httpstarting with version 2.2. The consolidated package ships a PSR-18 HTTP client (\InitPHP\HTTP\Client\Client) that callsext-curldirectly and is the supported way to issue HTTP requests in the InitPHP ecosystem.This repository is kept read-only for historical reference. No further updates will be released.
There is no drop-in replacement for
\InitPHP\Curl\Curl— the canonical client speaks PSR-7 / PSR-18 rather than exposing a thin cURL builder. Two paths exist:
You used this package to issue HTTP requests. Move to the canonical PSR-18 client:
// BeforeuseInitPHP\Curl\Curl; $curl = newCurl(); $curl->setUrl('https://example.com') ->setMethod('GET') ->handler(); $body = $curl->getResponse()['body']; // AfteruseInitPHP\HTTP\Client\Client; useInitPHP\HTTP\Message\Request; $client = newClient(); $response = $client->sendRequest(newRequest('GET', 'https://example.com')); $body = (string) $response->getBody();You used this package for non-HTTP cURL operations (FTP, SMTP, file uploads to custom endpoints, etc.). A thin wrapper around the cURL extension does not warrant its own package — call
curl_init()/curl_setopt()/curl_exec()directly, or pull in a more capable library (e.g.guzzlehttp/guzzle,symfony/http-client).Composer declares a
replacefrominitphp/http:^2.2to this package, so the two will not be installed side-by-side once you depend on the consolidated package.
Curl library to help you make advanced HTTP requests with PHP.
- PHP 7.4 or higher
- PHP Curl Extension
composer require initphp/curl
require_once"vendor/autoload.php";
use \InitPHP\Curl\Curl;
$curl = newCurl();
$curl->setUrl("https://example.com");
$curl->handler();
$res = $this->getResponse();
if(!empty($curl->getError())){
die($curl->getError());
}
echo$res['body'];This library can be used as HTTP client for your API service. Example:
require_once"vendor/autoload.php";
use \InitPHP\Curl\Curl;
$curl = Curl::client('PUT', 'http://api.service.example.com/update/1')
$curl->setVersion("1.1") // HTTP Version
->setHeader("Content-Type", "application/json")
->setBody(json_encode([
'username' => 'admin',
'password' => '12345',
]))
->handler();
if(!empty($curl->getError())){
die($curl->getError());
}
switch ($curl->getResponse('code')) {
case200 :
case201 :
// Successbreak;
case404 :
// Not Foundbreak;
case400:
// Badrequestbreak;
// ...
}Creates a new client object.
publicstaticfunction client(string$method, string$url): \InitPHP\Curl\Curl;Returns the result of the curl operation.
publicfunction getResponse(null|string$key = null): null|mixed;The values that can be used for the $key parameter and its explanation are explained below.
NULL: Returns an associative array containing all response information."status": Returns the status header information line of the HTTP response."code": Returns the HTTP response code."version": Returns HTTP response version information."headers": Returns the headers of the HTTP response as an array."body": Returns the body of the HTTP response.
Defines URL information for cURL.
publicfunction setUrl(string$url): selfThrows \InvalidArgumentException if it is not a valid URL.
Adds a header for the request.
publicfunction setHeader(string$name, string$value): selfExample :
/** @var \InitPHP\Curl\Curl $curl */$curl->setHeader('Content-type', 'application/json; charset=utf8');For the request; Adds one or more headers via an associative array.
publicfunction setHeaders(array$headers): selfExample :
/** @var \InitPHP\Curl\Curl $curl */$curl->setHeaders([
'Content-type' => 'application/json; charset=utf8'
]);Defines the HTTP Request method.
publicfunction setMethod(string$method = 'GET'): selfThe $method parameter can take the following values;
- GET, POST, PUT, HEAD, DELETE, PATCH, OPTIONS
HTTP Request is used to manually define the content information.
publicfunction setBody(string$body): selfDefines the HTTP version of the HTTP request.
publicfunction setVersion(string$httpVersion = '1.1'): selfThe $httpVersion parameter can take the following values;
1.01.12.0(libcurl v7.33 or higher version is required)
Defines the User Agent information to be reported to the server in the cURL process.
publicfunction setUserAgent(null|string$userAgent = null): selfExample :
/** @var \InitPHP\Curl\Curl $curl */$curl->setUserAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0");Defines HTTP referer information to be reported to the server.
publicfunction setReferer(null|string$referer = null): selfDefines whether requests follow redirects. If this method is not used, the redirects are not followed.
publicfunction setAllowRedirect(int$maxRedirect = 3): selfDefines the wait limit for the request in seconds or milliseconds.
publicfunction setTimeout(int$timeout = 0, bool$isMicrosecond = false): selfIf $isMicrosecond is FALSE, the value is used as seconds.
Defines a data to be sent as POST.
publicfunction setField(string$key, string$value): selfIt defines the data to be sent by POST with an associative array.
publicfunction setFields(array$fields) : selfDefines the SSL configurations of the cURL request.
publicfunction setSSL(int$host = 2, int$verify = 1, null|int$version = null): selfDefines the proxy to be used by cURL.
publicfunction setProxy($proxy): selfIt sends a file to be uploaded to the server.
publicfunction setUpload(string$name, \CURLFile$file): selfExample (Single):
/** @var \InitPHP\Curl\Curl $curl */$curl->setUpload("upload_file", new \CURLFile(__DIR__ . 'image.jpg')); // $_FILES["upload_file"]Example (Multi):
/** @var \InitPHP\Curl\Curl $curl */$curl->setUpload("upload_file[0]", new \CURLFile(__DIR__ . 'image-1.jpg'));
$curl->setUpload("upload_file[1]", new \CURLFile(__DIR__ . 'image-2.jpg'));It is the curl_getinfo() function in this library.
publicfunction getInfo(null|string$key = null): null|mixedIt is the curl_error() function in this library.
publicfunction getError(): null|stringIt is the curl_setopt() function in this library.
publicfunction setOpt(int$key, mixed$value): selfIt is the curl_setopt_array() function in this library.
publicfunction setOptions(array$options): selfcURL starts and executes.
publicfunction handler(): boolAfter cURL is handled, it writes the content to the specified file.
publicfunction save(string$filePath): false|intReturns the number of bytes written on success.
Example :
/** @var \InitPHP\Curl\Curl $curl */$curl = new \InitPHP\Curl\Curl();
$curl->setUrl("http://example.com")
->handler();
if($curl->save(__DIR__ . '/example.html') === FALSE){
echo"The file could not be written.";
}Defines a cookie to be sent with cURL.
publicfunction setCookie(string$name, string|int|float$value): selfExample :
/** @var \InitPHP\Curl\Curl $curl */$curl->setCookie('username', 'admin') // $_COOKIE['username']
->setCookie('mail', 'admin@example.com'); // $_COOKIE['mail']Defines cookies as multiple with an associative array.
publicfunction setCookies(array$cookies): selfExample :
/** @var \InitPHP\Curl\Curl $curl */$curl->setCookies([
'username' => 'admin', // $_COOKIE['username']'mail' => 'admin@example.com'// $_COOKIE['mail']
]);It tells the file path where the cookies values to be sent to the server will be kept or kept.
publicfunction setCookieJarFile(string$filePath): selfIf the specified file exists, cookies are read from the file and sent to the server. CURLOPT_COOKIEFILE
If the specified file does not exist, cookies from the server are written to the file. CURLOPT_COOKIEJAR
Example :
$login = new \InitPHP\Curl\Curl();
$login->setUrl("http://example.com/user/login")
->setField('username', 'admin')
->setField('password', '123456')
->setMethod('POST')
->setCookieJarFile(__DIR__ . '/session.txt')
->handler();
$dashboard = new \InitPHP\Curl\Curl();
$dashboard->setUrl("http://example.com/user/dashboard")
->setCookieJarFile(__DIR__ . '/session.txt')
->handler();Copyright © 2022 MIT License