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

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

mderakhshi/laravel-curl
=======================

Custom PHP Curl library for the Laravel 6

24PHP

Since Jun 3Pushed 5y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

mderakhshi/laravel-curl:
========================

[](#mderakhshilaravel-curl)

![license](https://camo.githubusercontent.com/c2a416338b4aca8c1092280d717caf99bd0fe8a7d15c37a72baefdf5186f89cf/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6d646572616b687368692f6c61726176656c2d6375726c2e737667)[![Latest Version on Packagist](https://camo.githubusercontent.com/cf15381df213fde659dce3d7361ffb48b5d4d43c399124a900076cbd50ff6a41/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d646572616b687368692f6c61726176656c2d6375726c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mderakhshi/laravel-curl)[![Total Downloads](https://camo.githubusercontent.com/e7e301786adc4492dbc234ab0e86b0c1d112e061d25fad80c97af226feda413f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d646572616b687368692f6c61726176656c2d6375726c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mderakhshi/laravel-curl)

fork:

Custom PHP cURL library for the Laravel 6 framework

The package provides an easy interface for sending cURL requests from your PHP web application. The package provides an intuitive, fluent interface similar the Laravel query builder to easily configure the request. Additionally, There are several utility methods that allow you to easily add certain options to the request. This makes it easier to create and use cURL requests and also makes your code more comprehensible.

The provided functionality is completely framework-independent but also contains a Laravel service provider for easy integration into your Laravel project.

> Note before posting an issue: When posting an issue for the package, always be sure to provide as much information regarding the request as possible. This includes the example cURL request you are trying to transfer into the package syntax, your actual package syntax (the full request) and (if possible) an example URL I can use to test the request myself if need be.

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

[](#installation)

run in terminal: `composer require mderakhshi/curl`

### Laravel 5.5+ Integration

[](#laravel-55-integration)

Laravel's package discovery will take care of integration for you.

### Laravel 5.\* Integration

[](#laravel-5-integration)

Add the service provider to your `config/app.php` file:

```
    'providers'     => array(

        //...
        mderakhshi\Curl\CurlServiceProvider::class,

    ),
```

Add the facade to your `config/app.php` file:

```
    'aliases'       => array(

        //...
        'Curl'          => mderakhshi\Curl\Facades\Curl::class,

    ),
```

### Laravel 4.\* Integration

[](#laravel-4-integration)

Add the service provider to your `app/config/app.php` file:

```
    'providers'     => array(

        //...
        'mderakhshi\Curl\CurlServiceProvider',

    ),
```

Add the facade to your `app/config/app.php` file:

```
    'facades'       => array(

        //...
        'Curl'          => 'mderakhshi\Curl\Facades\Curl',

    ),
```

### Lumen 5.\* integration

[](#lumen-5-integration)

In your `bootstrap/app.php`, make sure you've un-commented the following line (around line 26):

```
$app->withFacades();

```

Then, register your class alias:

```
class_alias('mderakhshi\Curl\Facades\Curl', 'Curl');

```

Finally, you have to register your ServiceProvider (around line 70-80):

```
/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/

// $app->register('App\Providers\AppServiceProvider');

// Package service providers
$app->register(mderakhshi\Curl\CurlServiceProvider::class);

```

### Integration without Laravel

[](#integration-without-laravel)

Create a new instance of the `CurlService` where you would like to use the package:

```
    $curlService = new \mderakhshi\Curl\CurlService();
```

Usage
-----

[](#usage)

### Laravel usage

[](#laravel-usage)

The package provides an easy interface for sending cURL requests from your application. The package provides a fluent interface similar the Laravel query builder to easily configure the request. There are several utility methods that allow you to easily add certain options to the request. If no utility method applies, you can also use the general `withOption`method.

> Helper:

```
   curl('https://www.google.com')->get();
   curl('https://www.google.com')->withData(['name'=>'ss'])->post();
```

### Sending GET requests

[](#sending-get-requests)

In order to send a `GET` request, you need to use the `get()` method that is provided by the package:

```
    use mderakhshi\Curl\Facades\Curl;

    // Send a GET request to: http://www.foo.com/bar
    $response = Curl::to('http://www.foo.com/bar')
        ->get();

    // Send a GET request to: http://www.foo.com/bar?foz=baz
    $response = Curl::to('http://www.foo.com/bar')
        ->withData( array( 'foz' => 'baz' ) )
        ->get();

    // Send a GET request to: http://www.foo.com/bar?foz=baz using JSON
    $response = Curl::to('http://www.foo.com/bar')
        ->withData( array( 'foz' => 'baz' ) )
        ->asJson()
        ->get();
```

### Sending POST requests

[](#sending-post-requests)

Post requests work similar to `GET` requests, but use the `post()` method instead:

```
    use mderakhshi\Curl\Facades\Curl;

    // Send a POST request to: http://www.foo.com/bar
    $response = Curl::to('http://www.foo.com/bar')
        ->post();

    // Send a POST request to: http://www.foo.com/bar
    $response = Curl::to('http://www.foo.com/bar')
        ->withData( array( 'foz' => 'baz' ) )
        ->post();

    // Send a POST request to: http://www.foo.com/bar with arguments 'foz' = 'baz' using JSON
    $response = Curl::to('http://www.foo.com/bar')
        ->withData( array( 'foz' => 'baz' ) )
        ->asJson()
        ->post();

    // Send a POST request to: http://www.foo.com/bar with arguments 'foz' = 'baz' using JSON and return as associative array
    $response = Curl::to('http://www.foo.com/bar')
        ->withData( array( 'foz' => 'baz' ) )
        ->asJson( true )
        ->post();
```

### Sending PUT requests

[](#sending-put-requests)

Put requests work similar to `POST` requests, but use the `put()` method instead:

```
    use mderakhshi\Curl\Facades\Curl;

    // Send a PUT request to: http://www.foo.com/bar/1 with arguments 'foz' = 'baz' using JSON
    $response = Curl::to('http://www.foo.com/bar/1')
       ->withData( array( 'foz' => 'baz' ) )
       ->asJson()
       ->put();
```

### Sending PATCH requests

[](#sending-patch-requests)

Patch requests work similar to `POST` requests, but use the `patch()` method instead:

```
    use mderakhshi\Curl\Facades\Curl;

    // Send a PATCH request to: http://www.foo.com/bar/1 with arguments 'foz' = 'baz' using JSON
    $response = Curl::to('http://www.foo.com/bar/1')
        ->withData( array( 'foz' => 'baz' ) )
        ->asJson()
        ->patch();
```

### Sending DELETE requests

[](#sending-delete-requests)

Delete requests work similar to `GET` requests, but use the `delete()` method instead:

```
    use mderakhshi\Curl\Facades\Curl;

    // Send a DELETE request to: http://www.foo.com/bar/1 using JSON
    $response = Curl::to('http://www.foo.com/bar/1')
        ->asJson()
        ->delete();
```

### Sending custom headers

[](#sending-custom-headers)

Sending custom headers is easy with the `withHeader()` method. Multiple calls can be chained together to add multiple headers to the request:

```
    use mderakhshi\Curl\Facades\Curl;

    // Send a GET request to: http://www.foo.com/bar with 2 custom headers
    $response = Curl::to('http://foo.com/bar')
        ->withHeader('MyFirstHeader: 123')
        ->withHeader('MySecondHeader: 456')
        ->get();
```

Alternatively, you can use the `withHeaders()` to combine multiple headers into one method call:

```
    use mderakhshi\Curl\Facades\Curl;

    // Send a GET request to: http://www.foo.com/bar with 2 custom headers
    $response = Curl::to('http://foo.com/bar')
        ->withHeaders( array( 'MyFirstHeader: 123', 'MySecondHeader: 456' ) )
        ->get();
```

You can also use key-value when using the `withHeaders()` method:

```
    use mderakhshi\Curl\Facades\Curl;

    // Send a GET request to: http://www.foo.com/bar with 2 custom headers
    $response = Curl::to('http://foo.com/bar')
        ->withHeaders( array( 'MyFirstHeader' => '123', 'MySecondHeader' => '456' ) )
        ->get();
```

> Note that headers will override each other if you add the same header more than once.

### Specifying the content type

[](#specifying-the-content-type)

Sending custom headers is easy with the `withContentType()` method. Multiple calls can be chained together to add multiple headers to the request:

```
    use mderakhshi\Curl\Facades\Curl;

    // Send a GET request to: http://www.foo.com/bar with a json content type
    $response = Curl::to('http://foo.com/bar')
        ->withContentType('application/json')
        ->get();
```

### Using proxies

[](#using-proxies)

If you need to send your requests via a proxy, you can use the 'withProxy()' method. The method takes five parameters:

- proxy url (required)
- port (optional)
- type of proxy scheme (optional, e.g. `http://`, `https://`, ...)
- username (optional)
- password (optional)

Optional parameters will be ignored if not filled in.

```
    use mderakhshi\Curl\Facades\Curl;

    // Send a GET request to: http://www.foo.com/bar with a json content type
    $response = Curl::to('http://foo.com/bar')
        ->withProxy('192.168.1.1', 80, 'http://', 'Foo', 'Bar')
        ->get();
```

### Sending files via Curl

[](#sending-files-via-curl)

For sending files via a POST request, you can use the `withFile` method to correctly format a request before sending:

```
    use mderakhshi\Curl\Facades\Curl;

    $response = Curl::to('http://foo.com/bar')
        ->withData( array( 'Foo' => 'Bar' ) )
        ->withFile( 'image_1', '/path/to/dir/image1.png', 'image/png', 'imageName1.png' )
        ->withFile( 'image_2', '/path/to/dir/image2.png', 'image/png', 'imageName2.png' )
        ->post();
```

You can add as many files to the request as you want. A couple of things to keep in mind:

- When submitting files, the `asJson()` method and `asJsonRequest()` method cannot be used. If you do, the files will not be transferred correctly
- The files are added to the data that was provided in the `withData()` method using the first parameter of the `withFile()` method. If this key already exists, it will be overridden.

### Downloading files

[](#downloading-files)

For downloading a file, you can use the `download()` method:

```
    use mderakhshi\Curl\Facades\Curl;

    // Download an image from: file http://www.foo.com/bar.png
    $response = Curl::to('http://foo.com/bar.png')
        ->withContentType('image/png')
        ->download('/path/to/dir/image.png');
```

### Using response objects

[](#using-response-objects)

By default, the package will only return the content of the request. In some cases, it might also be useful to know additional request information, such as the HTTP status code and error messages should they occur. In this case, you can use the `returnResponseObject()` method, which will return an stdClass that contains additional information as well as the response content:

```
    use mderakhshi\Curl\Facades\Curl;

    // Send a GET request to http://www.foo.com/bar and return a response object with additional information
    $response = Curl::to('http://www.foo.com/bar')
        ->returnResponseObject()
        ->get();

    $content = $response->content;
```

The response object will look like this:

```
{
   "content": "Message content here",
   "status": 200,
   "contentType": "content-type response header (ex: application/json)",
   "error": "Error message goes here (Only added if an error occurs)"
}
```

### Response headers

[](#response-headers)

In some cases it might be relevant to return the response headers back to the user. This can easily be done using the `withResponseHeaders()` method.

```
    use mderakhshi\Curl\Facades\Curl;

    // Send a GET request to http://www.foo.com/bar and return a response object with additional information including response headers
    $response = Curl::to('http://www.foo.com/bar')
        ->withResponseHeaders()
        ->returnResponseObject()
        ->get();

    $content = $response->content;
    $headers = $response->headers;
```

The response object will look like this:

```
{
    "content": "Message content here",
    "status": 200,
    "contentType": "content-type response header (ex: application/json)",
    "error": "Error message goes here (Only added if an error occurs)",
    "headers": {
        "header-type-1": "header-content-1",
        "header-type-2": "header-content-2"
    }
}
```

It is important to note that the `withResponseHeaders()` method must be used in conjunction with the `returnResponseObject()` method in order to see the returned headers

### Debugging requests

[](#debugging-requests)

In case a request fails, it might be useful to get debug the request. In this case, you can use the `enableDebug()` method. This method uses one parameter, which is the name of the file in which the debug information is to be stored:

```
    use mderakhshi\Curl\Facades\Curl;

    // Send a GET request to http://www.foo.com/bar and log debug information in /path/to/dir/logFile.txt
    $response = Curl::to('http://www.foo.com/bar')
        ->enableDebug('/path/to/dir/logFile.txt')
        ->get();
```

### Using cURL options

[](#using-curl-options)

You can add various cURL options to the request using of several utility methods such as `withHeader()` for adding a header to the request, or use the general `withOption()` method if no utility method applies. The package will automatically prepend the options with the `CURLOPT_` prefix. It is worth noting that the package does not perform any validation on the cURL options. Additional information about available cURL options can be found [here](http://php.net/manual/en/function.curl-setopt.php).

MethodDefault valueDescriptionwithTimeout()30 secondsSet the timeout of the request (integer or float)allowRedirect()falseAllow the request to be redirected internallyasJsonRequest()falseSubmit the request data as JSONasJsonResponse()falseDecode the response data from JSONasJson()falseUtility method to set both `asJsonRequest()` and `asJsonResponse()` at the same timewithHeader()stringAdd an HTTP header to the requestwithHeaders()arrayAdd multiple HTTP headers to the requestwithContentType()noneSet the content type of the responsewithFile()noneAdd a file to the form data to be sentcontainsFile()falseShould be used to submit files through formswithData()arrayAdd an array of data to sent with the request (GET or POST)setCookieFile()noneSet a file to read cookies fromsetCookieJar()noneSet a file to store cookies inwithOption()noneGeneric method to add any cURL option to the requestFor specific information regarding parameters and return types, I encourage you to take a look at `mderakhshi\curl\src\mderakhshi\Curl\Builder.php`. This class has extensive doc blocks that contain all the necessary information for each specific method.

### Usage without Laravel

[](#usage-without-laravel)

Usage without Laravel is identical to usage described previously. The only difference is that you will not be able to use the facades to access the `CurlService`.

```
    $curlService = new \mderakhshi\Curl\CurlService();

    // Send a GET request to: http://www.foo.com/bar
    $response = $curlService->to('http://www.foo.com/bar')
        ->get();

    // Send a POST request to: http://www.foo.com/bar
    $response = $curlService->to('http://www.foo.com/bar')
        ->post();

    // Send a PUT request to: http://www.foo.com/bar
    $response = $curlService->to('http://www.foo.com/bar')
        ->put();

    // Send a DELETE request to: http://www.foo.com/bar
    $response = $curlService->to('http://www.foo.com/bar')
        ->delete();
```

Planning
--------

[](#planning)

- Add additional utility methods for other cURL options
- Add contract to allow different HTTP providers such as Guzzle

License
-------

[](#license)

This package is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity33

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/03b3cbbbf98cd8159da3618646e7986b1d5690f8093cd86e1ad0890b9dc6a404?d=identicon)[m-derakhshi](/maintainers/m-derakhshi)

---

Top Contributors

[![m-derakhshi](https://avatars.githubusercontent.com/u/59544612?v=4)](https://github.com/m-derakhshi "m-derakhshi (5 commits)")

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/mderakhshi-laravel-curl/health.svg)](https://phpackages.com/packages/mderakhshi-laravel-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)
