Laravel CURL Request Example using ixudra/curl
In this post we will give you information about Laravel CURL Request Example using ixudra/curl. Hear we will give you detail about Laravel CURL Request Example using ixudra/curlAnd how to use it also give you demo for it if it is necessary.
If you know about php curl then you can simply run get, post request and get data from url. php curl is very interesting things. if you know and use in with php then you have to do curl_ini(), curl_setopt(), curl_exec() and curl_close() etc that way we can simply run request.
But, If you require to fire curl request in laravel 5 application then you don’t require to do curl_init(), curl_close() etc. We can run request very easily and get response in json. so we will use ixudra/curl composer package for run curl request very simple.
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. So let’s just follow bellow example:
Install ixudra/curl Package:
First of all, we have to add ixudra/curl package for run curl request method so one your cmd or terminal and fire bellow command:
composer require ixudra/curl
After successfully install package, open config/app.php file and add service provider and alias.
config/app.php
'providers' => [
....
IxudraCurlCurlServiceProvider::class,
],
'aliases' => [
....
'Curl' => IxudraCurlFacadesCurl::class,
]
Add New Route:
next we need to create route for run get curl request. so open your routes/web.php file and add following route.
routes/web.php
Route::get('get-curl', 'HomeController@getCURL');
Add Controller Method:
Here, we will add new getCURL() in HomeController so if you don’t have HomeController then create and add following method.
app/Http/Controllers/HomeController.php
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use IxudraCurlFacadesCurl;
class HomeController extends Controller
{
/**
* Show the application dashboard.
*
* @return IlluminateHttpResponse
*/
public function getCURL()
{
$response = Curl::to('https://jsonplaceholder.typicode.com/posts')
->get();
dd($response);
}
}
Now, you can simply run above example and see you will get result 100 data in json, it is very simple as you see, you don’t require to anymore use php curl and very easily run curl request.
So you can also run post, put, patch, delete request as bellow example, let’s just need to change getCURL() on home page:
CURL Post Request:
public function getCURL()
{
$response = Curl::to('https://example.com/posts')
->withData(['title'=>'Test', 'body'=>'sdsd', 'userId'=>1])
->post();
dd($response);
}
CURL Put Request:
public function getCURL()
{
$response = Curl::to('https://example.com/posts/1')
->withData(['title'=>'Test', 'body'=>'sdsd', 'userId'=>1])
->put();
dd($response);
}
CURL Patch Request:
public function getCURL()
{
$response = Curl::to('https://example.com/posts/1')
->withData(['title'=>'Test', 'body'=>'sdsd', 'userId'=>1])
->patch();
dd($response);
}
CURL Delete Request:
public function getCURL()
{
$response = Curl::to('https://example.com/posts/1')
->delete();
dd($response);
}
As above requests we can do it very simply. So you can also get more information about curl package from here : ixudra/curl.
I hope you will find your solution.
Hope this code and post will helped you for implement Laravel CURL Request Example using ixudra/curl. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs