Skip to content
This repository was archived by the owner on May 24, 2026. It is now read-only.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

14 Commits

Repository files navigation

InitPHP Curl

⚠️ DEPRECATED — Use initphp/http instead

As part of the InitPHP package consolidation, this package has been retired in favor of initphp/http starting with version 2.2. The consolidated package ships a PSR-18 HTTP client (\InitPHP\HTTP\Client\Client) that calls ext-curl directly 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.

Migration

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:

  1. 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();
  2. 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 replace from initphp/http:^2.2 to 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.

Latest Stable VersionTotal DownloadsLatest Unstable VersionLicensePHP Version Require

Requirements

  • PHP 7.4 or higher
  • PHP Curl Extension

Installation

composer require initphp/curl

Usage

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;
// ...
}

Methods

client()

Creates a new client object.

publicstaticfunction client(string$method, string$url): \InitPHP\Curl\Curl;

getResponse()

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.

setUrl()

Defines URL information for cURL.

publicfunction setUrl(string$url): self

Throws \InvalidArgumentException if it is not a valid URL.

setHeader()

Adds a header for the request.

publicfunction setHeader(string$name, string$value): self

Example :

/** @var \InitPHP\Curl\Curl $curl */$curl->setHeader('Content-type', 'application/json; charset=utf8');

setHeaders()

For the request; Adds one or more headers via an associative array.

publicfunction setHeaders(array$headers): self

Example :

/** @var \InitPHP\Curl\Curl $curl */$curl->setHeaders([
'Content-type' => 'application/json; charset=utf8'
]);

setMethod()

Defines the HTTP Request method.

publicfunction setMethod(string$method = 'GET'): self

The $method parameter can take the following values;

  • GET, POST, PUT, HEAD, DELETE, PATCH, OPTIONS

setBody()

HTTP Request is used to manually define the content information.

publicfunction setBody(string$body): self

setVersion()

Defines the HTTP version of the HTTP request.

publicfunction setVersion(string$httpVersion = '1.1'): self

The $httpVersion parameter can take the following values;

  • 1.0
  • 1.1
  • 2.0 (libcurl v7.33 or higher version is required)

setUserAgent()

Defines the User Agent information to be reported to the server in the cURL process.

publicfunction setUserAgent(null|string$userAgent = null): self

Example :

/** @var \InitPHP\Curl\Curl $curl */$curl->setUserAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0");

setReferer()

Defines HTTP referer information to be reported to the server.

publicfunction setReferer(null|string$referer = null): self

setAllowRedirect()

Defines whether requests follow redirects. If this method is not used, the redirects are not followed.

publicfunction setAllowRedirect(int$maxRedirect = 3): self

setTimeout()

Defines the wait limit for the request in seconds or milliseconds.

publicfunction setTimeout(int$timeout = 0, bool$isMicrosecond = false): self

If $isMicrosecond is FALSE, the value is used as seconds.

setField()

Defines a data to be sent as POST.

publicfunction setField(string$key, string$value): self

setFields()

It defines the data to be sent by POST with an associative array.

publicfunction setFields(array$fields) : self

setSSL()

Defines the SSL configurations of the cURL request.

publicfunction setSSL(int$host = 2, int$verify = 1, null|int$version = null): self

setProxy()

Defines the proxy to be used by cURL.

publicfunction setProxy($proxy): self

setUpload()

It sends a file to be uploaded to the server.

publicfunction setUpload(string$name, \CURLFile$file): self

Example (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'));

getInfo()

It is the curl_getinfo() function in this library.

publicfunction getInfo(null|string$key = null): null|mixed

getError()

It is the curl_error() function in this library.

publicfunction getError(): null|string

setOpt()

It is the curl_setopt() function in this library.

publicfunction setOpt(int$key, mixed$value): self

setOptions()

It is the curl_setopt_array() function in this library.

publicfunction setOptions(array$options): self

handler()

cURL starts and executes.

publicfunction handler(): bool

save()

After cURL is handled, it writes the content to the specified file.

publicfunction save(string$filePath): false|int

Returns 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.";
}

setCookie()

Defines a cookie to be sent with cURL.

publicfunction setCookie(string$name, string|int|float$value): self

Example :

/** @var \InitPHP\Curl\Curl $curl */$curl->setCookie('username', 'admin') // $_COOKIE['username']
->setCookie('mail', 'admin@example.com'); // $_COOKIE['mail']

setCookies()

Defines cookies as multiple with an associative array.

publicfunction setCookies(array$cookies): self

Example :

/** @var \InitPHP\Curl\Curl $curl */$curl->setCookies([
'username' => 'admin', // $_COOKIE['username']'mail' => 'admin@example.com'// $_COOKIE['mail']
]);

setCookieJarFile()

It tells the file path where the cookies values to be sent to the server will be kept or kept.

publicfunction setCookieJarFile(string$filePath): self

If 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();

Credits

License

Copyright © 2022 MIT License

About

Curl library to help you make advanced HTTP requests with PHP.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages