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

ActiveYii2-extension[HTTP &amp; Networking](/categories/http)

linslin/yii2-curl
=================

Easy and nice cURL extension with RESTful support for Yii2

1.5.0(5y ago)1811.5M—7.1%90[6 PRs](https://github.com/linslin/Yii2-Curl/pulls)20MITPHPPHP &gt;=7.2.0 &lt;9.0

Since Nov 13Pushed 2y ago13 watchersCompare

[ Source](https://github.com/linslin/Yii2-Curl)[ Packagist](https://packagist.org/packages/linslin/yii2-curl)[ RSS](/packages/linslin-yii2-curl/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (6)Versions (26)Used By (20)

yii2-curl extension
===================

[](#yii2-curl-extension)

[![Latest Stable Version](https://camo.githubusercontent.com/6fd1584bfc010a2df6834e4300d222bf8775152ca67edd54c5cf928d1dc8afda/68747470733a2f2f706f7365722e707567782e6f72672f6c696e736c696e2f796969322d6375726c2f762f737461626c65)](https://packagist.org/packages/linslin/yii2-curl)[![Latest Master Build](https://camo.githubusercontent.com/94770c356bb3751a69121c80311110b8027b9f8b44bff070eed7d229b7c09e62/68747470733a2f2f6170692e7472617669732d63692e6f72672f6c696e736c696e2f596969322d4375726c2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/linslin/Yii2-Curl/builds)[![Test Coverage](https://camo.githubusercontent.com/67af0e078d50b2f04833ca3bd33a3fb0b2b94bed1223e166fcfb5cf5dbd2bdc4/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6c696e736c696e2f596969322d4375726c2f6261646765732f636f7665726167652e737667)](https://codeclimate.com/github/linslin/Yii2-Curl/coverage)[![Total Downloads](https://camo.githubusercontent.com/caedb6d2f651fa308d399677ce42cf17f0d3f292f8d1e7d3de23d283ef92c1ab/68747470733a2f2f706f7365722e707567782e6f72672f6c696e736c696e2f796969322d6375726c2f646f776e6c6f616473)](https://packagist.org/packages/linslin/yii2-curl)[![License](https://camo.githubusercontent.com/b24295d82ff6c9f36bfad0dbe558c5bb8903408339c9453bfe73d0e1ef127ef9/68747470733a2f2f706f7365722e707567782e6f72672f6c696e736c696e2f796969322d6375726c2f6c6963656e7365)](https://packagist.org/packages/linslin/yii2-curl)

Easy working cURL extension for Yii2, including RESTful support:

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

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

[](#requirements)

- Yii2
- PHP &gt;=7.2.0 || 8.0.1
- ext-curl, ext-json, and php-curl installed

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

```
composer require --prefer-dist linslin/yii2-curl "*"
```

Usage
-----

[](#usage)

Once the extension is installed, simply use it in your code. The following example shows you how to handling a simple GET Request.

```
use linslin\yii2\curl;
$curl = new curl\Curl();

//get http://example.com/
$response = $curl->get('http://example.com/');

if ($curl->errorCode === null) {
   echo $response;
} else {
     // List of curl error codes here https://curl.haxx.se/libcurl/c/libcurl-errors.html
    switch ($curl->errorCode) {

        case 6:
            //host unknown example
            break;
    }
}
```

```
// GET request with GET params
// http://example.com/?key=value&scondKey=secondValue
$curl = new curl\Curl();
$response = $curl->setGetParams([
        'key' => 'value',
        'secondKey' => 'secondValue'
     ])
     ->get('http://example.com/');
```

```
// POST URL form-urlencoded
$curl = new curl\Curl();
$response = $curl->setPostParams([
        'key' => 'value',
        'secondKey' => 'secondValue'
     ])
     ->post('http://example.com/');
```

```
// POST RAW JSON
$curl = new curl\Curl();
$response = $curl->setRawPostData(
     json_encode[
        'key' => 'value',
        'secondKey' => 'secondValue'
     ])
     ->post('http://example.com/');
```

```
// POST RAW JSON and auto decode JSON respawn by setting raw = true.
// This is usefull if you expect an JSON response and want to autoparse it.
$curl = new curl\Curl();
$response = $curl->setRawPostData(
     json_encode[
        'key' => 'value',
        'secondKey' => 'secondValue'
     ])
     ->post('http://example.com/', true);

// JSON decoded response by parsing raw = true in to ->post().
var_dump($response);
```

```
// POST RAW XML
$curl = new curl\Curl();
$response = $curl->setRawPostData('Test')
     ->post('http://example.com/');
```

```
// POST with special headers
$curl = new curl\Curl();
$response = $curl->setPostParams([
        'key' => 'value',
        'secondKey' => 'secondValue'
     ])
     ->setHeaders([
        'Custom-Header' => 'user-b'
     ])
     ->post('http://example.com/');
```

```
// POST JSON with body string & special headers
$curl = new curl\Curl();

$params = [
    'key' => 'value',
    'secondKey' => 'secondValue'
];

$response = $curl->setRequestBody(json_encode($params))
     ->setHeaders([
        'Content-Type' => 'application/json',
        'Content-Length' => strlen(json_encode($params))
     ])
     ->post('http://example.com/');
```

```
// Avanced POST request with curl options & error handling
$curl = new curl\Curl();

$params = [
    'key' => 'value',
    'secondKey' => 'secondValue'
];

$response = $curl->setRequestBody(json_encode($params))
     ->setOption(CURLOPT_ENCODING, 'gzip')
     ->post('http://example.com/');

// List of status codes here http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
switch ($curl->responseCode) {

    case 'timeout':
        //timeout error logic here
        break;

    case 200:
        //success logic here
        break;

    case 404:
        //404 Error logic here
        break;
}

//list response headers
var_dump($curl->responseHeaders);
```

Testing
-------

[](#testing)

- Run codeception tests with `vendor/bin/codecept run` in repository root dir. Run codeception with clover report `XDEBUG_MODE=coverage vendor/bin/codecept run --coverage-xml ./../../build/logs/clover.xml`. On windows run `vendor\bin\codecept.bat run`. On windows with clover report run `vendor\bin\codecept.bat run --coverage-xml ./../../build/logs/clover.xml`.

Changelog
---------

[](#changelog)

---

##### Release 1.5.0 - Changelog

[](#release-150---changelog)

- Added PHP 8 support.
- Updated phiremock to v2.
- Removed not needed dependencies from the composer package file ([\#88](https://github.com/linslin/Yii2-Curl/issues/88)).
- Fixed CURLFile object serialization for profiling ([\#87](https://github.com/linslin/Yii2-Curl/issues/87)).

##### Release 1.4.0 - Changelog

[](#release-140---changelog)

- Added support for HTTP-Method OPTIONS.
- Removed deprecated PHP Version support. Minimum PHP Version is now 7.2.0. Please use version "linslin/yii2-curl 1.3.0" -  if you need PHP 7.1.0+ support.

##### Release 1.3.0 - Changelog

[](#release-130---changelog)

- Fixed HTTP-Method parsing on PATCH request.
- Updated DocBlocks + code refactoring.
- Removed deprecated PHP Version support. Minimum PHP Version is now 7.1.3. Please use version "linslin/yii2-curl 1.2.1" -  if you need PHP 5.4+ support.

##### Release 1.2.2 - Changelog

[](#release-122---changelog)

- Added some new cURL examples into readme.md.

##### Release 1.2.1 - Changelog

[](#release-121---changelog)

- Added `setRawPostData([mixed]) [this]` which allows you to post any data format.

##### Release 1.2.0 - Changelog

[](#release-120---changelog)

- Added `unsetHeader([string header]) [this]` helper which allows you to unset one specific header.
- Added `setHeader([string header, string value]) [this]` helper which allows you to set one specific header.
- Added `getRequestHeaders() [array]` helper which returns all request headers as an array.
- Added `getRequestHeader([string headerKey]) [string|null]` helper which returns a specific request header as an string.
- Added new test cases for `getRequestHeaders()` and `getRequestHeader()`.
- Readme adjustments.

##### Release 1.1.3 - Changelog

[](#release-113---changelog)

- Fixed issue with patch request.
- Fully added functionalTests for 100% coverage.

##### Release 1.1.2 - Changelog

[](#release-112---changelog)

- Fixed [\#59](https://github.com/linslin/Yii2-Curl/issues/59)
- Fixed [\#57](https://github.com/linslin/Yii2-Curl/issues/57)

##### Release 1.1.1 - Changelog

[](#release-111---changelog)

- Fixed wrong parameter parsing into `_httpRequest()` (thanks to yemexx1)
- Added JSON decode functions tests (thanks to yemexx1)

##### Release 1.1.0 - Changelog

[](#release-110---changelog)

- Added `setHeaders() [array]` helper.
- Added `setPostParams() [array]` helper.
- Added `setGetParams() [array]` helper.
- Added `setRequestBody() [string]` helper.
- Added `getUrl()` helper.
- Added API attribute `errorText [string|null]` - holds a string describing the given error code - [\#49](https://github.com/linslin/Yii2-Curl/issues/49).
- Added functionTests to ensure stability.
- Allow PHP class goodness - [\#52](https://github.com/linslin/Yii2-Curl/issues/52).
- Fixed header explode - [\#51](https://github.com/linslin/Yii2-Curl/pull/51).

##### Release 1.0.11 - Changelog

[](#release-1011---changelog)

- Added API attribute `responseHeaders [array|null]` which returns an array of all response headers.
- Changed `_defaultOptions[CURLOPT_HEADER]` to `true`.
- Profile debugging is only active if constant `YII_DEBUG` is `true`.

##### Release 1.0.10 - Changelog

[](#release-1010---changelog)

- Fixed PHP notice [\#39](https://github.com/linslin/Yii2-Curl/issues/39).

##### Release 1.0.9 - Changelog

[](#release-109---changelog)

- Added API attribute `responseCode [string|null]` which holds the HTTP response code.
- Added API attribute `responseCharset [string|null]` which holds the response charset.
- Added API attribute `responseLength [integer|null]` which holds the response length.
- Added API attribute `errorCode` which holds the a integer code error like described here: .
- Fixed Issue .
- Fixed Issue  and removed exception throwing on curl fail. This allow the user to handle the error while using attribute `errorCode`.

##### Release 1.0.8 - Changelog

[](#release-108---changelog)

- Added API method `setOptions([array])` which allows to setup multiple options at once.
- Fixed Issue [\#30](https://github.com/linslin/Yii2-Curl/issues/30).

##### Release 1.0.7 - Changelog

[](#release-107---changelog)

- Fixed `getInfo([, int $opt = 0 ])` exception were cURL wasn't initialized before calling `getInfo($opt)`.

##### Release 1.0.6 - Changelog

[](#release-106---changelog)

- Added `getInfo([, int $opt = 0 ])` method to retrieve  data.

##### Release 1.0.5 - Changelog

[](#release-105---changelog)

- Made `body` callback not depending on HTTP-Status codes anymore. You can retrieve `body` data on any HTTP-Status now.
- Fixed Issue [\#19](https://github.com/linslin/Yii2-Curl/issues/19) where override default settings break options.
- Added timeout response handling. `$curl->responseCode = 'timeout'`

##### Release 1.0.4 - Changelog

[](#release-104---changelog)

- `CURLOPT_RETURNTRANSFER` is now set to true on default - [\#18](https://github.com/linslin/Yii2-Curl/issues/18)
- Readme.md adjustments.

##### Release 1.0.3 - Changelog

[](#release-103---changelog)

- Fixed override of user options. [\#7](https://github.com/linslin/Yii2-Curl/pull/7)
- Nice formatted PHP-examples.
- Moved `parent::init();` behavior into unitTest Controller.

##### Release 1.0.2 - Changelog

[](#release-102---changelog)

- Added custom params support
- Added custom status code support
- Added POST-Param support and a readme example
- Removed "body" support at request functions. Please use "CURLOPT\_POSTFIELDS" to setup a body now.
- Readme modifications

##### Release 1.0.1 - Changelog

[](#release-101---changelog)

- Removed widget support
- Edited some spellings + added more examples into readme.md

##### Release 1.0 - Changelog

[](#release-10---changelog)

- Official stable release

Thanks to
---------

[](#thanks-to)

---

Mariano Custiel ([@mcustiel](https://github.com/mcustiel))

... and all other contributors.

###  Health Score

55

—

FairBetter than 98% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity58

Moderate usage in the ecosystem

Community36

Small or concentrated contributor base

Maturity91

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 92.7% 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 ~120 days

Recently: every ~326 days

Total

20

Last Release

1914d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/7883954c930171c2dbfd069379e9fea311b0480dfd4bebf3ade9c393be217b02?d=identicon)[linslin](/maintainers/linslin)

---

Top Contributors

[![linslin](https://avatars.githubusercontent.com/u/6159031?v=4)](https://github.com/linslin "linslin (115 commits)")[![hoksilato](https://avatars.githubusercontent.com/u/2535342?v=4)](https://github.com/hoksilato "hoksilato (2 commits)")[![vitalyzhakov](https://avatars.githubusercontent.com/u/1775220?v=4)](https://github.com/vitalyzhakov "vitalyzhakov (2 commits)")[![qingdi](https://avatars.githubusercontent.com/u/5287422?v=4)](https://github.com/qingdi "qingdi (1 commits)")[![roslov](https://avatars.githubusercontent.com/u/6573063?v=4)](https://github.com/roslov "roslov (1 commits)")[![mcustiel](https://avatars.githubusercontent.com/u/3268370?v=4)](https://github.com/mcustiel "mcustiel (1 commits)")[![erickskrauch](https://avatars.githubusercontent.com/u/4787256?v=4)](https://github.com/erickskrauch "erickskrauch (1 commits)")[![Dzhuneyt](https://avatars.githubusercontent.com/u/1754428?v=4)](https://github.com/Dzhuneyt "Dzhuneyt (1 commits)")

---

Tags

curlyii2extensionrestful

### Embed Badge

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

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

###  Alternatives

[hiqdev/yii2-hiart

ActiveRecord for API

5951.8k3](/packages/hiqdev-yii2-hiart)[joni-jones/yii2-wschat

Online chat based on web sockets and ratchet php

981.3k](/packages/joni-jones-yii2-wschat)[yiiplus/yii2-websocket

使用yii2封装 websocket 扩展

212.6k](/packages/yiiplus-yii2-websocket)

PHPackages © 2026

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