Provides REST request methods. Mapper bundle for php internal curl library

Installs: 556 572

Dependents: 10

Suggesters: 0

Security: 0

Stars: 57

Watchers: 8

Forks: 19

Open Issues: 12


2.0.3 2019-01-21 09:27 UTC

This package is not auto-updated.

Last update: 2024-04-13 14:45:09 UTC


A Smart REST client with an intuitive API, providing all REST methods and returning a Symfony Response Object.


There are some Symfony2 bundles providing abstractions for REST requests, but none of them is actually feeling like: "That's what I was looking for!". Having used some of these in the past, we always had to improve or remap their API's for our own needs.

Some days ago we faced the same frustrating challenge again and started to meditate about the idea that our specific API needs really aren't that particular. So we questioned ourselves how a simple REST-Client-API should look like:

$restclient->post($url, $payload);
$restclient->put($url, $payload);

Another concern was the leaky abstraction many of the other bundles present to us in respect to the output: They map the PHP-native curl-addon's API to an object-oriented interface only to let us work with the non-object-oriented original output of curl's API calls. At this point it is more convenient to stay with the (extremely inconvenient) PHP internal curl API only.

But how should outputs be wrapped?

  • So by visiting this page you are probably a Symfony developer
  • Thus we can infer that you know about the Symfony Response Object

So the Symfony Response Object is our choice to go: We don't need to roll out our own implementation and can stay within the boundaries of our framework of choice - win/win :-).

So all in all let's call it a day and start goin' gorillas with this one.


Step 1: Download the bundle using composer

Add the bundle by running the command:

php composer.phar require ci/restclientbundle

Composer will install the bundle to your project's vendor/ci directory.

Step 2: Enable the bundle

Enable the bundle in the symfony kernel

// PROJECTROOT/app/AppKernel.php

public function registerBundles()
    $bundles = array(
        // ...
        new Circle\RestClientBundle\CircleRestClientBundle(),


The bundle allows you to configure all default options that the underlying PHP internal curl library provides - with their real names. No re-re-re-mapping-mapping-re-... :D

The names and their possible values can be found here:

You can change the configuration by adding the following lines to your app/config.yml:

        $optionName: $value
        $optionName: $value


        CURLOPT_HTTPHEADER:     [ 'Content-Type: application/json' ]

Sets thre request header to application/json and follows redirects.


You cannot change the default value for CURLOPT_RETURNTRANSFER (default=true).


$restClient = $this->container->get('circle.restclient');

$restClient->post('', 'somePayload');
$restClient->put('', 'somePayload');
$restClient->patch('', 'somePayload');

$restClient->options('', 'somePayload');

Exception Handling

As the rest client is using libcurl we have created an exception class for each libcurl error. Have a look at the full list of errors here: The exception class representing a libcurl error has the following naming conventions:

  • Imagine there's the error named CURLE_OPERATION_TIMEDOUT
  • Afterward change the spelling into camel case: OPERATION_TIMEDOUT --> OperationTimedOut
  • Add the postfix "Exception": OperationTimedOut --> OperationTimedOutException
  • The OperationTimedOutException is the exception corresponding to the libcurl error CURLE_OPERATION_TIMEDOUT

Knowing that all these exceptions exist improves exception handling a lot:

try {
} catch (Circle\RestClientBundle\Exceptions\OperationTimedOutException $exception) {
  // do something

If you still want to catch all rest exceptions catch the basic libcurl exception:

try {
} catch (Circle\RestClientBundle\Exceptions\CurlException $exception) {
  // do something

Advanced usage

You can add additional options to customize a specific request by adding an option array as key value store.

$restClient = $this->container->get('circle.restclient');

$restClient->get('', array(CURLOPT_CONNECTTIMEOUT => 30));
$restClient->post('', 'somePayload', array(CURLOPT_CONNECTTIMEOUT => 30));
$restClient->put('', 'somePayload', array(CURLOPT_CONNECTTIMEOUT => 30));
$restClient->delete('', array(CURLOPT_CONNECTTIMEOUT => 30));

$restClient->head('', array(CURLOPT_CONNECTTIMEOUT => 30));
$restClient->options('', 'somePayload', array(CURLOPT_CONNECTTIMEOUT => 30));
$restClient->trace('', array(CURLOPT_CONNECTTIMEOUT => 30));
$restClient->connect('', array(CURLOPT_CONNECTTIMEOUT => 30));

Testing the bundle

The bundle can be tested via phpunit.


  • Vendors must be installed via composer
  • (Of course) Phpunit must be installed
  • Port 8000 must not be blocked on the local machine
  • XDebug should be enabled

Executing tests

The Tests are executed against a local php server located in the Tests/TestServer folder. Execute the tests via



  • Strict rules for rest methods such as server MUST NOT return a message-body in the response for HEAD requests
  • EventHandling (onRequest, preRequest, postRequest)