skip to Main Content
Laravel 5 CURL Request Using Ixudra/curl Package

Laravel 5 CURL request using ixudra/curl package

Today we are going to learn the easiest way to sending Laravel 5 CURL request. We can easily send the CURL request using the ixudra curl package with fluent interface similar the Laravel query builder. The ixudra curl package using the CURL for sending the request.

We can send the curl request using PHP way like

<?php 
        // create curl resource 
        $ch = curl_init(); 

        // set url 
        curl_setopt($ch, CURLOPT_URL, "example.com"); 

        //return the transfer as a string 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

        // $output contains the output string 
        $output = curl_exec($ch); 

        // close curl resource to free up system resources 
        curl_close($ch);      
?>

We are initialise CURL request set the URL for sending the CURL request getting the output form CURL Object then close CURL request. This way we can send the CURL request.

We are going to learn a new way of sending CURL request using ixudra curl package.

I hope you installed the Laravel if not please install using this article.

First of all, We are going to install ixudra curl package.

composer require ixudra/curl

After, Complete the package installation we are going to registering service provider and aliases on Laravel.

Let’s open “config/app.php” file for registering service provide and aliases like

'providers'     => array(
    Ixudra\Curl\CurlServiceProvider::class,
),
'aliases'       => array(
        'Curl'          => Ixudra\Curl\Facades\Curl::class,
),

The package is ready for use, Let’s see the example for sending the curl request using ixudra curl package.

Let’s see the example of sending the CURL request using ixuda package. First of all, We are going to create the route for the CURL example like

Route::get('wordpress/home','WordpressController@getHomeCurl');

Now, Let’s create the “WordPressController” using below command.

php artisan make:controller WordPressController

That command help us for creating the controller file, Now, let’s open that controller file for CURL request like

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Ixudra\Curl\Facades\Curl;

class WordPressController extends Controller
{

    public function getPosts(){

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

        echo '<pre>';
        print_r($response);
        exit;

        // Send a GET request to: https://www.thecodingstuff.com?search=text
        $response = Curl::to('https://thecodingstuff.com')
        ->withData( array( 'search' => 'test' ) )
        ->get();

        // Send a GET request to: https://www.thecodingstuff.com/?search=text using JSON
        $response = Curl::to('https://thecodingstuff.com')
        ->withData( array( 'search' => 'text' ) )
        ->asJson()
        ->get();

    }

}

We can be sending the different type of request using ixudra curl package like GET, POST, PUT, PATCH, DELETE or else We can send the custom headers or content types. You can get more detail from here.

GET CURL

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

POST CURL

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

PUT CURL

// Send a PUT request to: http://www.thecodingstuff.com
$response = Curl::to('https://www.thecodingstuff.com')
   ->put();

PATCH CURL

// Send a PATCH request to: http://www.thecodingstuff.com 
$response = Curl::to('https://www.thecodingstuff.com')  
    ->patch();

DELETE CURL

// Send a DELETE request to: https://www.thecodingstuff.com
$response = Curl::to('http://www.thecodingstuff.com')
    ->delete();

The ixudra package provides functionality we can set custom headers like

// Send a GET request to: https://www.thecodingstuff.com with custom headers
$response = Curl::to('http://foo.com/bar')
    ->withHeader('customheader: thecodingstuff')
    ->get();

We can easily send the CURL request using ixudra Laravel Package. If you face any issue write the comment below.

Saroj

I am a web designer and developer part-time blogger. Write something on some free time. Find my every day thought here :)

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top