Laravel curl request using ixudra/curl package
Have a look some curl built in function
cURL GET request example code
ou can test cURL in your own local server as it’s the same with using a regular form with an action.
Use json_decode if the return is in json format.
But in this tutorial i am going to show you curl request example in laravel 6. But, If you require to fire curl request in laravel 6 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.
We can also fetch data from url in JavaScript using XMLHttpRequest. See the below example
If you run this code, then you will see the fetch data from this url. By the way, that is not our concern, cause we are going to make it in laravel. 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 let's just follow bellow example:
Step 1 : Install ixudra/curl Package:
First of all we have to install this package. Cause we will check it using this package. So run below command
After successfully install package, open config/app.php file and add service provider and alias.
config/app.php
Step 2 : Add Route:
After that we need to create route for run get curl request. so open your routes/web.php file and add following route.
routes/web.php
Step 3 : Add Controller Method:
Here, we will add new getData() in HomeController so if you don't have HomeController then create and add following method.
app/Http/Controllers/HomeController.php
Now, you can simply run above example and see you will get result of this above url which we already fetch from xmlHttpRequest.
So you can also run post, put, patch, delete request as bellow example, let's just need to change getData() on home page:
CURL Post Request:
CURL Put Request:
CURL Patch Request:
CURL Delete Request:
you can also get more information about curl package from here : ixudra/curl.
Hope this curl requests tutorial will help you.