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

ActiveLibrary[HTTP &amp; Networking](/categories/http)

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

Curl Library

1.0(3y ago)24411MITPHPPHP &gt;=7.4

Since Jul 1Pushed 3y ago1 watchersCompare

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

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

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

[](#initphp-curl)

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

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity47

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

Unknown

Total

1

Last Release

1413d 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 (10 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

[friendsofsymfony/rest-bundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony

2.8k73.3M319](/packages/friendsofsymfony-rest-bundle)[php-http/discovery

Finds and installs PSR-7, PSR-17, PSR-18 and HTTPlug implementations

1.3k309.5M1.2k](/packages/php-http-discovery)[nyholm/psr7

A fast PHP7 implementation of PSR-7

1.3k235.4M2.4k](/packages/nyholm-psr7)[pusher/pusher-php-server

Library for interacting with the Pusher REST API

1.5k94.8M293](/packages/pusher-pusher-php-server)[spatie/crawler

Crawl all internal links found on a website

2.8k16.3M52](/packages/spatie-crawler)[react/http

Event-driven, streaming HTTP client and server implementation for ReactPHP

78126.4M414](/packages/react-http)

PHPackages © 2026

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