Laravel Curl



You can use choose a composer dependency instead of a CURL request to simplify and extend functionality. Laravel FCM is a great package available. This package is available with lots of functionality to send different types of notifications. Using ixudra curl package you can very simply curl post request in laravel, curl get request in laravel, curl put request in laravel, curl delete request in laravel etc. So here you can also set header, data etc. Here first i will install package on laravel application then we will give you very simple example to get json data.

cURL is a solid and simple tool that allows transferring data from and to any server with command line using various protocols including HTTP. Curl is a command-line tool that allows us to do HTTP requests from shell. It also covers many other protocols, like FTP, though they go beyond the scope of this tutorial.

In this tutorials we will share with you how to make cUrl request in laravel, this is most common topic and functionality. help of cUrl you can make any type request like GET, POST, OPTION etc... but basically in this tutorials we show you 2 most common HTTP request with cUrl.

cUrl is mostly use in laravel when you work with any third party API. in laravel application cUrl is provide very easy way to functionality for call any API with any HTTP method. most off developer choose cUrl for call API as well as make any API.

Make GET Request

Make POST Request

We hope these small tutorials help everyone. if you like this article then please comment below. Thanks..

LaravelLaravel Curl

Make Curl Request

Was this article helpful?

0 out of 0 person found this article helpful.

Custom PHP Curl library for the Laravel framework - developed by Ixudra

Maintainers
Details

Installs: 1 420 816

Dependents: 102

Suggesters: 0

Security: 0

Stars: 541

Watchers: 27

Forks: 132

Requires

  • php: >=5.4.0
  • ext-curl: *
  • illuminate/support: >=4.0

Replaces

None

This package is auto-updated.

Last update: 2021-04-22 13:58:58 UTC

Custom PHP cURL library for the Laravel 4 or 5 framework - developed by Ixudra.

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

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

Laravel Curl

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

Installation

Pull this package in through Composer.

or run in terminal:composer require ixudra/curl

Laravel 5.5+ Integration

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

Laravel 5.* Integration

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

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

Laravel 4.* Integration

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

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

Lumen 5.* integration

Laravel

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

Then, register your class alias:

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

Integration without Laravel

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

Usage

Laravel usage

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

Sending GET requests

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

Sending POST requests

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

Sending PUT requests

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

Sending PATCH requests

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

Sending DELETE requests

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

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:

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

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

For (bearer) authorization headers, you can also use specific utility methods:

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

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:

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.

Sending files via Curl

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

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

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

Using response objects

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

The response object will look like this:

Response headers

Where To Buy Curlformers Cheap

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.

The response object will look like this:

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

Laravel Curl

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:

Using cURL options

You can add various cURL options to the request using of several utility methods such as withHeader() for adding aheader to the request, or use the general withOption() method if no utility method applies. The package willautomatically prepend the options with the CURLOPT_ prefix. It is worth noting that the package does not performany validation on the cURL options. Additional information about available cURL options can be foundhere.

MethodDefault valueDescription
withTimeout()30 secondsSet the timeout of the request (integer or float)
withConnectTimeout()30 secondsSet the connect timeout of the request (integer or float)
allowRedirect()falseAllow the request to be redirected internally
asJsonRequest()falseSubmit the request data as JSON
asJsonResponse()falseDecode the response data from JSON
asJson()falseUtility method to set both asJsonRequest() and asJsonResponse() at the same time
withHeader()stringAdd an HTTP header to the request
withHeaders()arrayAdd multiple HTTP headers to the request
withContentType()noneSet the content type of the response
withFile()noneAdd a file to the form data to be sent
containsFile()falseShould be used to submit files through forms
withData()arrayAdd an array of data to sent with the request (GET or POST)
setCookieFile()noneSet a file to read cookies from
setCookieJar()noneSet a file to store cookies in
withOption()noneGeneric method to add any cURL option to the request

For specific information regarding parameters and return types, I encourage you to take a look atixudracurlsrcIxudraCurlBuilder.php. This class has extensive doc blocks that contain all the necessary informationfor each specific method.

Usage without Laravel

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

Planning

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

Support

Help me further develop and maintain this package by supporting me via Patreon!!

License

This package is open-sourced software licensed under the MIT license

Contact

For package questions, bug, suggestions and/or feature requests, please use the Github issue system and/or submit a pull request. When submitting an issue, always provide a detailed explanation of your problem, any response or feedback your get, log messages that might be relevant as well as a source code example that demonstrates the problem. If not, I will most likely not be able to help you with your problem. Please review the contribution guidelines before submitting your issue or pull request.

For any other questions, feel free to use the credentials listed below:

Jan Oris (developer)

  • Email: jan.oris@ixudra.be
  • Telephone: +32 496 94 20 57