Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

list down the use of curl in laravel

HTTP GET Request:

Make a basic HTTP GET request to an external API and retrieve data.

$ch = curl_init('https://api.example.com/data');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
Enter fullscreen mode Exit fullscreen mode

HTTP POST Request with Form Data:

Send data to an API using an HTTP POST request with form parameters.

$postData = ['key1' => 'value1', 'key2' => 'value2'];
$ch = curl_init('https://api.example.com/post-endpoint');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($ch);
curl_close($ch);
Enter fullscreen mode Exit fullscreen mode
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class YourController extends Controller
{
    public function makePostRequest()
    {
        // URL endpoint for the POST request
        $url = 'https://example.com/api/endpoint';

        // Form data to be sent in the POST request
        $formData = [
            'key1' => 'value1',
            'key2' => 'value2',
            // Add more key-value pairs as needed
        ];

        // Initialize cURL session
        $ch = curl_init();

        // Set cURL options
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $formData);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // Execute cURL session and get the response
        $response = curl_exec($ch);

        // Check for cURL errors
        if (curl_errno($ch)) {
            // Handle error
            echo 'cURL Error: ' . curl_error($ch);
        }

        // Close cURL session
        curl_close($ch);

        // Process the response
        // For example, you can return it as a response from the controller
        return response()->json(['response' => $response]);
    }
}
Enter fullscreen mode Exit fullscreen mode

HTTP POST Request with JSON Data:

Send JSON data to an API using an HTTP POST request.

$jsonData = json_encode(['key' => 'value']);
$ch = curl_init('https://api.example.com/post-json-endpoint');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$response = curl_exec($ch);
curl_close($ch);
Enter fullscreen mode Exit fullscreen mode

Handling HTTP Headers:

Include custom headers in your cURL request.

$ch = curl_init('https://api.example.com/data');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer YOUR_ACCESS_TOKEN']);
$response = curl_exec($ch);
curl_close($ch);
Enter fullscreen mode Exit fullscreen mode

File Upload with POST Request:

Upload a file to a server using an HTTP POST request.

$fileData = ['file' => new \CURLFile('/path/to/file.txt')];
$ch = curl_init('https://api.example.com/upload');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fileData);
$response = curl_exec($ch);
curl_close($ch);
Enter fullscreen mode Exit fullscreen mode

Handling Cookies:

Send and receive cookies in your cURL request.

$ch = curl_init('https://api.example.com/data');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIE, 'cookie_name=cookie_value');
$response = curl_exec($ch);
curl_close($ch);
Enter fullscreen mode Exit fullscreen mode

Following Redirects:

Allow cURL to automatically follow redirects.

$ch = curl_init('https://api.example.com/redirecting-endpoint');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
curl_close($ch);
Enter fullscreen mode Exit fullscreen mode

Asynchronous Requests:

Perform asynchronous requests using cURL multi handles.

$ch1 = curl_init('https://api.example.com/endpoint1');
$ch2 = curl_init('https://api.example.com/endpoint2');

$mh = curl_multi_init();
curl_multi_add_handle($mh, $ch1);
curl_multi_add_handle($mh, $ch2);

do {
    curl_multi_exec($mh, $running);
} while ($running > 0);

curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
Enter fullscreen mode Exit fullscreen mode

Setting Timeout for Requests:

Set a timeout for cURL requests to avoid waiting indefinitely.

$ch = curl_init('https://api.example.com/data');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // Set timeout to 10 seconds
$response = curl_exec($ch);
curl_close($ch);
Enter fullscreen mode Exit fullscreen mode

HTTP DELETE Request:

Make an HTTP DELETE request to an API.

$ch = curl_init('https://api.example.com/resource-to-delete');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
$response = curl_exec($ch);
curl_close($ch);
Enter fullscreen mode Exit fullscreen mode

These examples cover a variety of common use cases for working with cURL in Laravel, from basic requests to more advanced scenarios. Depending on your specific needs, you can adapt these examples to suit your Laravel application.

Summary

HTTP GET Request
curl_init==>curl_setopt==>curl_exec==>curl_close
HTTP POST Request with Form Data
curl_init==>curl_setopt(for api)==>curl_setopt(for postfield)==>curl_exec==>curl_close
HTTP POST Request with JSON Data:
curl_init==>curl_setopt(for api)==>curl_setopt(for postfield)==>curl_setopt(Content-Type)==>curl_exec==>curl_close
Handling HTTP Headers
File Upload with POST Request
Handling CookiesSend and receive cookies in your cURL requesT)
Following RedirectsSetting Timeout for Requests
Setting Timeout for Requests( to avoid waiting indefinitely)

Top comments (0)