PHPackages                             initphp/curl - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. initphp/curl

Abandoned → [initphp/http](/?search=initphp%2Fhttp)ArchivedLibrary[HTTP &amp; Networking](/categories/http)

initphp/curl
============

Curl Library (DEPRECATED — use initphp/http:^2.2 for HTTP requests)

1.0.1(1mo ago)3512[1 PRs](https://github.com/InitPHP/Curl/pulls)1MITPHPPHP &gt;=7.4

Since Jul 1Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/InitPHP/Curl)[ Packagist](https://packagist.org/packages/initphp/curl)[ RSS](/packages/initphp-curl/feed)WikiDiscussions main Synced today

READMEChangelog (2)DependenciesVersions (3)Used By (1)

InitPHP Curl
============

[](#initphp-curl)

> ## ⚠️ DEPRECATED — Use [`initphp/http`](https://github.com/InitPHP/HTTP) instead
>
> [](#️-deprecated--use-initphphttp-instead)
>
> As part of the InitPHP package consolidation, **this package has been retired in favor of [`initphp/http`](https://github.com/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
>
> [](#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:
>
>     ```
>     // Before
>     use InitPHP\Curl\Curl;
>     $curl = new Curl();
>     $curl->setUrl('https://example.com')
>          ->setMethod('GET')
>          ->handler();
>     $body = $curl->getResponse()['body'];
>
>     // After
>     use InitPHP\HTTP\Client\Client;
>     use InitPHP\HTTP\Message\Request;
>
>     $client   = new Client();
>     $response = $client->sendRequest(new Request('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 Version](https://camo.githubusercontent.com/71f30854009bc556400058e6eea66e84780af893020a56326832d800485315ff/687474703a2f2f706f7365722e707567782e6f72672f696e69747068702f6375726c2f76)](https://packagist.org/packages/initphp/curl) [![Total Downloads](https://camo.githubusercontent.com/1e8e565baf0bf9f293ec4962603ed77c54b4657ab0571f9f02ae06c5095e6a74/687474703a2f2f706f7365722e707567782e6f72672f696e69747068702f6375726c2f646f776e6c6f616473)](https://packagist.org/packages/initphp/curl) [![Latest Unstable Version](https://camo.githubusercontent.com/84609f2fc14460a83da5475e528a406f9170600d4a11aff69dae9021ad13dcfd/687474703a2f2f706f7365722e707567782e6f72672f696e69747068702f6375726c2f762f756e737461626c65)](https://packagist.org/packages/initphp/curl) [![License](https://camo.githubusercontent.com/9bb4045f7e1c685718afa7e41090d7b6fe3a123c801d69a6c15a47f9a98f6017/687474703a2f2f706f7365722e707567782e6f72672f696e69747068702f6375726c2f6c6963656e7365)](https://packagist.org/packages/initphp/curl) [![PHP Version Require](https://camo.githubusercontent.com/ed9ada0afd101959462aa562afe8ce12fd4ebd5a21aa87e1f98c40209b110d47/687474703a2f2f706f7365722e707567782e6f72672f696e69747068702f6375726c2f726571756972652f706870)](https://packagist.org/packages/initphp/curl)

Requirements
------------

[](#requirements)

- PHP 7.4 or higher
- PHP Curl Extension

Installation
------------

[](#installation)

```
composer require initphp/curl

```

Usage
-----

[](#usage)

```
require_once "vendor/autoload.php";
use \InitPHP\Curl\Curl;

$curl = new Curl();
$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')) {
    case 200 :
    case 201 :
        // Success
        break;
    case 404 :
        // Not Found
        break;
    case 400:
        // Badrequest
        break;
    // ...
}
```

Methods
-------

[](#methods)

### `client()`

[](#client)

Creates a new client object.

```
public static function client(string $method, string $url): \InitPHP\Curl\Curl;
```

### `getResponse()`

[](#getresponse)

Returns the result of the curl operation.

```
public function 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()`

[](#seturl)

Defines URL information for cURL.

```
public function setUrl(string $url): self
```

Throws `\InvalidArgumentException` if it is not a valid URL.

### `setHeader()`

[](#setheader)

Adds a header for the request.

```
public function setHeader(string $name, string $value): self
```

**Example :**

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

### `setHeaders()`

[](#setheaders)

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

```
public function setHeaders(array $headers): self
```

**Example :**

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

### `setMethod()`

[](#setmethod)

Defines the HTTP Request method.

```
public function setMethod(string $method = 'GET'): self
```

The `$method` parameter can take the following values;

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

### `setBody()`

[](#setbody)

HTTP Request is used to manually define the content information.

```
public function setBody(string $body): self
```

### `setVersion()`

[](#setversion)

Defines the HTTP version of the HTTP request.

```
public function 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()`

[](#setuseragent)

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

```
public function 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()`

[](#setreferer)

Defines HTTP referer information to be reported to the server.

```
public function setReferer(null|string $referer = null): self
```

### `setAllowRedirect()`

[](#setallowredirect)

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

```
public function setAllowRedirect(int $maxRedirect = 3): self
```

### `setTimeout()`

[](#settimeout)

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

```
public function setTimeout(int $timeout = 0, bool $isMicrosecond = false): self
```

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

### `setField()`

[](#setfield)

Defines a data to be sent as POST.

```
public function setField(string $key, string $value): self
```

### `setFields()`

[](#setfields)

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

```
public function setFields(array $fields) : self
```

### `setSSL()`

[](#setssl)

Defines the SSL configurations of the cURL request.

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

### `setProxy()`

[](#setproxy)

Defines the proxy to be used by cURL.

```
public function setProxy($proxy): self
```

### `setUpload()`

[](#setupload)

It sends a file to be uploaded to the server.

```
public function 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()`

[](#getinfo)

It is the `curl_getinfo()` function in this library.

```
public function getInfo(null|string $key = null): null|mixed
```

### `getError()`

[](#geterror)

It is the `curl_error()` function in this library.

```
public function getError(): null|string
```

### `setOpt()`

[](#setopt)

It is the `curl_setopt()` function in this library.

```
public function setOpt(int $key, mixed $value): self
```

### `setOptions()`

[](#setoptions)

It is the `curl_setopt_array()` function in this library.

```
public function setOptions(array $options): self
```

### `handler()`

[](#handler)

cURL starts and executes.

```
public function handler(): bool
```

### `save()`

[](#save)

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

```
public function 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()`

[](#setcookie)

Defines a cookie to be sent with cURL.

```
public function 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()`

[](#setcookies)

Defines cookies as multiple with an associative array.

```
public function setCookies(array $cookies): self
```

**Example :**

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

### `setCookieJarFile()`

[](#setcookiejarfile)

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

```
public function 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
-------

[](#credits)

- [Muhammet ŞAFAK](https://www.muhammetsafak.com.tr) &lt;&gt;

License
-------

[](#license)

Copyright © 2022 [MIT License](./LICENSE)

###  Health Score

44

—

FairBetter than 90% of packages

Maintenance92

Actively maintained with recent releases

Popularity14

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1423 days

Total

2

Last Release

41d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4b6b34f3ac8938d8ee52ba3bd260680855dc5715c7b2929d9380de30d15a67dd?d=identicon)[muhammetsafak](/maintainers/muhammetsafak)

---

Top Contributors

[![muhammetsafak](https://avatars.githubusercontent.com/u/104234499?v=4)](https://github.com/muhammetsafak "muhammetsafak (11 commits)")

---

Tags

curlphpphp-curlphp-curl-library

### Embed Badge

![Health badge](/badges/initphp-curl/health.svg)

```
[![Health](https://phpackages.com/badges/initphp-curl/health.svg)](https://phpackages.com/packages/initphp-curl)
```

###  Alternatives

[php-http/cache-plugin

PSR-6 Cache plugin for HTTPlug

25126.1M82](/packages/php-http-cache-plugin)[illuminate/http

The Illuminate Http package.

11937.9M6.9k](/packages/illuminate-http)[rdkafka/rdkafka

A PHP extension for Kafka

2.2k24.3k1](/packages/rdkafka-rdkafka)[httpsoft/http-message

Strict and fast implementation of PSR-7 and PSR-17

87965.9k114](/packages/httpsoft-http-message)[mezzio/mezzio-router

Router subcomponent for Mezzio

265.4M92](/packages/mezzio-mezzio-router)[serpapi/google-search-results-php

Get Google, Bing, Baidu, Ebay, Yahoo, Yandex, Home depot, Naver, Apple, Duckduckgo, Youtube search results via SerpApi.com

69127.2k](/packages/serpapi-google-search-results-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
