Laravel’s ixudra package offers an effective method for making HTTP requests using curl.

Use curl to send get and post requests by performing the following actions:

  1. Install the package ixudra/curl.
  2. Adjust the installed package’s settings.
  3. In your controller code, import the class.
  4. Use curl to send get and post requests.

Install the ixudra/curl package

Installing ixudra/curl for your application using a composer command is possible and is demonstrated below:

composer require ixudra/curl

Configure the installed package

Your app.php file should now include the providers and aliases. The config directory must contain the app.php file.

'providers' => [

    ....

    Ixudra\Curl\CurlServiceProvider::class,

],

'aliases' => [

    ....

    'Curl' => Ixudra\Curl\Facades\Curl::class,

]

Import the class in your controller

You must include the class in your controller file as shown below in order to add it to your controller:

use Ixudra\Curl\Facades\Curl;

Usage

Create a method called getCurl() in the controller where you wish to implement the curl .

Note: You can give the method whatever name you choose, but getCurl() is used in this example.

Get request

Let’s use this package to make a get() request.

public function getCURL()
{
$response = Curl::to('https://jsonplaceholder.typicode.com/posts')
                    ->get();
dd($response);
}

The get request is sent in the code above using the Curl:: facade and the to() method, and the response is then dumped.

In your situation, you might wish to execute the logic necessary for your application rather than dumping the result.

Post request

public function getCURL()

{

    $response = Curl::to('https://example.com/posts')

                ->withData(['title'=>'Test', 'body'=>'sdsd', 'userId'=>1])

                ->post();

    dd($response);

}

The post request is made in the code above using the to() method, and the withData parameter specifies what data must be provided. The response is dropped once more.

The put request in the code above can be replaced with the patch, put, or delete requests.


Recommended Posts

View All

Laravel Dropbox api File Upload example using league/flysystem-dropbox


Learn how to upload files to Dropbox using Laravel and league/flysystem-dropbox in this step-by-step tutorial. Improve your Laravel skills today!

Laravel 9 Highcharts Example Tutorial


In this tutorial, I'll give you an example of how to use highchart js to construct a highchart in a laravel 9 application.

Laravel 9 Upload image using Summernote editor


A basic Javascript package called Summernote Editor can be used to create online WYSIWYG web editors.

Laravel 9 PHP Guzzle Http Client Examples


We will provide answers in this manual. If you've been searching the internet for a Laravel Guzzle http client example, your search is over.

Laravel 9 Socialite Login with Facebook Tutorial


In this tutorial, we'll learn how to use the Socialite package in Laravel to login with a Facebook social networking account.