When developing an application, you will at some point need to send cURL requests.
In Laravel, you can send a cURL request either using the default PHP way or using the Guzzle HTTP client package.
In this article, I show how you can easily send cURL request with less code using the HTTP client and read the return response in Laravel.
Contents
- Install Guzzle HTTP client
- Create API
- Send GET request
- Send POST request
- Send request with Header
- Conclusion
1. Install Guzzle HTTP client
Using composer to install the package –
composer require guzzlehttp/guzzle
2. Create API
For example purpose, I have created api.php
file to handle cURL request.
Returning JSON response based on GET or POST request –
- If
$request == 1
means it is GET type then store name and city in$response
Array and return in JSON format. - If
$request == 2
means it is POST type then read POST data and assign them to the$response
Array and return in JSON format.
Completed Code
<?php $request = 0; if(isset($_GET['request'])){ $request = $_GET['request']; } // GET request if($request == 1){ $response = array(); $response['name'] = "Rohit singh"; $response['city'] = "Indore"; echo json_encode($response); die; } // POST request if($request == 2){ // Read POST data $data = json_decode(file_get_contents("php://input")); $name = $data->name; $city = $data->city; $response['name'] = $name; $response['city'] = $city; echo json_encode($response); die; }
3. Send GET request
Syntax –
$response = Http::get([ URL ]);
- Open a controller where you want to write cURL request.
- In the example, I am using
PagesController
. - Include
use Illuminate\Support\Facades\Http;
. - Creating
getrequest()
method.
Send request –
- Specify request URL in
$apiURL
. - Pass
$apiURL
inHttp::get()
–
$response = Http::get($apiURL);
Read response –
- HTTP request returns instance of
Response
. - Using the
Response
instance read status, ok, successful, failed, body, object, and json. - I am reading response data in
Object
format and assign to the variables.
Completed Code
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Http; class PagesController extends Controller { public function getrequest(){ $apiURL = 'http://localhost/apis/request.php?request=1'; $response = Http::get($apiURL); // Read response $data['status'] = $response->status(); $data['ok'] = $response->ok(); $data['successful'] = $response->successful(); $data['failed'] = $response->failed(); $data['body'] = $response->body(); $data['object'] = $response->object(); $data['json'] = $response->json(); dd($data); // Read Object response if($response->ok()){ $resdata = $response->object(); $name = $resdata->name; $city = $resdata->city; } } }
Output –
4. Send POST request
Syntax –
$response = Http::post([ URL ], [ // Data ]);
- I am using the same
PagesController
to send POST cURL request. - Include
use Illuminate\Support\Facades\Http;
. - Create
postrequest()
method.
Send request –
- Pass
$apiURL
inHttp::post()
and sending name and city as data.
$response = Http::post($apiURL, [ 'name' => 'Yogesh singh', 'city' => 'Bhopal' ]);
Read response –
- Reading step is the same as GET request response.
- I am reading the response in
JSON
instead of Object and assign to the variables.
Completed Code
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Http; class PagesController extends Controller { public function postrequest(){ $apiURL = 'http://localhost/apis/request.php?request=2'; $response = Http::post($apiURL, [ 'name' => 'Yogesh singh', 'city' => 'Bhopal' ]); // Read response $data['status'] = $response->status(); $data['ok'] = $response->ok(); $data['successful'] = $response->successful(); $data['failed'] = $response->failed(); $data['body'] = $response->body(); $data['object'] = $response->object(); $data['json'] = $response->json(); dd($data); // Read JSON response if($response->ok()){ $resdata = $response->json(); $name = $resdata['name']; $city = $resdata['city']; } } }
Output –
5. Send request with Header
Using the same POST request code and add header using withHeaders()
method.
I have specified 2 headers – Content-Type
and Accept
.
Completed Code
public function postrequest(){ $apiURL = 'http://localhost/apis/request.php?request=2'; $response = Http::withHeaders([ 'Content-Type' => 'application/json', 'Accept' => 'application/json' ])->post($apiURL, [ 'name' => 'Yogesh singh', 'city' => 'Bhopal' ]); // Read response $data['status'] = $response->status(); $data['ok'] = $response->ok(); $data['successful'] = $response->successful(); $data['failed'] = $response->failed(); $data['body'] = $response->body(); $data['object'] = $response->object(); $data['json'] = $response->json(); dd($data); // Read JSON response if($response->ok()){ $resdata = $response->json(); $name = $resdata['name']; $city = $resdata['city']; } }
6. Conclusion
I hope you now have an understanding of the Guzzle HTTP client package in Laravel and how you can use it to send cURL requests in your application.
If you are interested in learning more about HTTP Client, you can follow this link.
If you found this tutorial helpful then don't forget to share.