PHPackages                             codezero/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. codezero/curl

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

codezero/curl
=============

Simple cURL wrapper

1.1.1(11y ago)042.4k1MITPHPPHP &gt;=5.4.0

Since Aug 22Pushed 11y ago2 watchersCompare

[ Source](https://github.com/codezero-be/curl)[ Packagist](https://packagist.org/packages/codezero/curl)[ RSS](/packages/codezero-curl/feed)WikiDiscussions master Synced today

READMEChangelog (3)Dependencies (1)Versions (4)Used By (1)

Simple cURL Wrapper
===================

[](#simple-curl-wrapper)

![GitHub release](https://camo.githubusercontent.com/5994fa77143e34d4fccae9ef5b04e6fabf6f931a48cf4b378ad0413d74c6b8bb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f636f64657a65726f2d62652f6375726c2e737667)![License](https://camo.githubusercontent.com/b81762bae0aa6a4960320db700135386a93dd3f23abba401f5c9d41c9d6bca5e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f636f64657a65726f2f6375726c2e737667)[![Build Status](https://camo.githubusercontent.com/252d7268bc40d7b3a8040e724f8a072f83d0f2bf698d0b49bc482deea1b03f1d/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f636f64657a65726f2d62652f6375726c2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/codezero-be/curl)[![Scrutinizer](https://camo.githubusercontent.com/694d565ad5d5c418acb37b3bd5bfd702466af5e616ae879e3a80fc625bec22e8/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f636f64657a65726f2d62652f6375726c2e737667)](https://scrutinizer-ci.com/g/codezero-be/curl)[![Total Downloads](https://camo.githubusercontent.com/15a1b26b7d9b8fe96c38c3dfdd6eb9c3b3deb798b261f698f41ff4ab7a9ec236/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636f64657a65726f2f6375726c2e737667)](https://packagist.org/packages/codezero/curl)

This package wraps most of the cURL functions in a dedicated `Curl` class, making it feel more object oriented and a little easier to use.

More importantly, the `Request` class provides some friendly methods that will set the most commonly used cURL options for you, so you don't have to memorize these.

This is a **simple** cURL wrapper. Some features are not supported (yet):

- Including headers in the response output (`CURLOPT_HEADER`)
- cURL multi handles
- cURL share handles

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

[](#installation)

You can download this package, or install it through Composer:

```
"require": {
	"codezero/curl": "1.*"
}

```

Usage
-----

[](#usage)

Send requests, the easy way!

### Create a `Request` instance:

[](#create-a-request-instance)

```
$request = new \CodeZero\Curl\Request();

```

You can now use this for all of your requests. You don't need to create a new instance for every request.

### Configure the request:

[](#configure-the-request)

```
$url = 'http://my.site/api';
$data = ['do' => 'something', 'with' => 'this']; //=> Optional
$headers = ['Some Header' => 'Some Value']; //=> Optional

```

If you want you can set custom cURL options, before sending the request:

```
$request->setOption(CURLOPT_USERAGENT, 'My User Agent');

```

... or unset one:

```
$request->unsetOption(CURLOPT_USERAGENT);

```

> **NOTE:** Only the URL, data and headers option, and a few options that are required for the given request method will be reset automatically on every request. Custom options will remain set until you unset them. An overview of all cURL options can be found here: [http://php.net/manual/en/function.curl-setopt.php](http://php.net/manual/en/function.curl-setopt.php "cURL options")

### Send the request: (one of the following)

[](#send-the-request-one-of-the-following)

```
$response = $request->get($url, $data, $headers);
$response = $request->post($url, $data, $headers);
$response = $request->put($url, $data, $headers);
$response = $request->patch($url, $data, $headers);
$response = $request->delete($url, $data, $headers);

```

All of these methods will return an instance of the `CodeZero\Curl\Response` class.

### Get the response body

[](#get-the-response-body)

```
$body = $response->getBody();

```

### Get additional request info

[](#get-additional-request-info)

Fetch an array with all info:

```
$info = $response->info()->getList();

```

Fetch specific information:

```
$httpCode = $response->info()->getHttpCode(); //=> "200"
$responseType = $response->info()->getResponseType(); //=> "application/json"
$responseCharset = $response->info()->getResponseCharset(); //=> "UTF-8"

```

> **TIP:** For an overview of all the available info, take a look at the `CodeZero\Curl\ResponseInfo` class or refer to [http://php.net/manual/en/function.curl-getinfo.php](http://php.net/manual/en/function.curl-getinfo.php "cURL info")

Exceptions
----------

[](#exceptions)

#### cURL issues

[](#curl-issues)

A `CodeZero\Curl\CurlException` will be thrown, if there was a problem initializing cURL. This will probably be the case if the cURL extension is not loaded by PHP, or if there is some other local server issue.

#### Request issues

[](#request-issues)

A `CodeZero\Curl\RequestException` will be thrown, if cURL was unable to execute the request. This might be the case if your request is not properly configured (unsupported protocol, etc.). Find more information about these kind of errors on [http://curl.haxx.se/libcurl/c/libcurl-errors.html](http://curl.haxx.se/libcurl/c/libcurl-errors.html "cURL errors")

#### Response issues

[](#response-issues)

HTTP response errors &gt;= 400 will not throw an exception, unless you set the `CURLOPT_FAILONERROR` cURL option to `true`. If you do, these errors will also throw a `CodeZero\Curl\RequestException`, with the proper error message.

Another way to handle HTTP errors is to check the `$httpCode` value:

```
if ($httpCode >= 400)
{
	// Handle error...
	// or throw your own exception...
}

```

Curl Class
----------

[](#curl-class)

You can also use the `Curl` class instead of the `Request` class. The difference is that you will need to provide all of the cURL options yourself and you will just get the raw response back instead of the `Response` object.

### Quick example:

[](#quick-example)

```
$options = [
    CURLOPT_URL => 'http://my.site/api',
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_HTTPGET => true,
    CURLOPT_RETURNTRANSFER => true
];

$curl = new \CodeZero\Curl\Curl();

// Send & get results
$responseBody = $curl->sendRequest($options); //=> Returns the actual output
$info = $curl->getRequestInfo(); //=> Returns info array

// Get errors
$error = $curl->getError(); //=> For PHP < 5.5.0 this is the same as $curl->getErrorDescription()
$errorCode = $curl->getErrorCode();
$errorDescription = $curl->getErrorDescription();

// Close cURL resource
$curl->close();

```

Options are **not reset** automatically after each request. If you need to reset them, you can either run `$curl->close()` to force a new cURL resource to be initialized at the next request, or you can call `$curl->reset()`.

### Long example:

[](#long-example)

The following will do the exact same thing as the previous example:

```
$options = [
    CURLOPT_URL => 'http://my.site/api',
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_HTTPGET => true,
    CURLOPT_RETURNTRANSFER => true
];

$curl = new \CodeZero\Curl\Curl();

// Setup & send
$curl->initialize();
$curl->setOptions($options);
$curl->sendRequest();

// Get results
$responseBody = $curl->getResponse(); //=> Returns the actual output
$info = $curl->getRequestInfo(); //=> Returns info array

// Get errors
$error = $curl->getError();//=> For PHP < 5.5.0 this is the same as $curl->getErrorDescription()
$errorCode = $curl->getErrorCode();
$errorDescription = $curl->getErrorDescription();

// Close cURL resource
$curl->close();

```

### Error handling:

[](#error-handling)

The `Curl` class will only throw a `CodeZero\Curl\CurlException`, when there is some cURL initialization issue. You will need to check the error code to determine if the request had any problems:

```
if ($errorCode > 0)
{
	// Do something...
}

```

Testing
-------

[](#testing)

```
$ vendor/bin/phpspec run

```

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

---

[![Analytics](https://camo.githubusercontent.com/cbed7c31daee122c4811ffb573fdac14b787fac7c79e53faf10bed3d5bb96c5e/68747470733a2f2f67612d626561636f6e2e61707073706f742e636f6d2f55412d35383837363031382d312f636f64657a65726f2d62652f6375726c)](https://github.com/igrigorik/ga-beacon)

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity60

Established project with proven stability

 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 ~90 days

Total

3

Last Release

4151d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2e17b7a892452367dfb0e5c55bf04844a16bb781f356f50019332d4b9a476ec6?d=identicon)[codezero](/maintainers/codezero)

---

Top Contributors

[![ivanvermeyen](https://avatars.githubusercontent.com/u/3598622?v=4)](https://github.com/ivanvermeyen "ivanvermeyen (15 commits)")

---

Tags

httprequestapicurl

### Embed Badge

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

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

###  Alternatives

[nategood/httpful

A Readable, Chainable, REST friendly, PHP HTTP Client

1.8k17.7M275](/packages/nategood-httpful)[aplus/http-client

Aplus Framework HTTP Client Library

2171.6M1](/packages/aplus-http-client)[vinelab/http

An http library developed for the laravel framework. aliases itself as HttpClient

59302.6k11](/packages/vinelab-http)

PHPackages © 2026

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