Send PHP cURL POST Request in Codeigniter 4 Tutorial

CURL is the love language used to express its feeling for making network requests through the command-line tool. When it comes to transferring data across the networks, this is the tool you need.

The great thing about cURL is that it supports innumerable protocols (standard communication protocol) like HTTP, HTTPS, FTP, SMTP, or many more with magnificent fidelity.

In this PHP cURL POST request example, we will see the ramifications of our Codeigniter knowledge.

You will learn how to send PHP cURL post requests in Codeigniter 4 app; furthermore, learn to deal with headers with authentication profoundly.

Being a novice has its benefits; you always have the boldness to grow your awareness about solidifying your learning experience.

Similarly, in this PHP Curl POST in Codeigniter tutorial, we will learn and, most importantly, try to assimilate everything about PHP curl functions to handle POST, PUT, GET and Delete requests courageously and systematically.

After completing this agile example of send PHP curl post requests in Codeigniter 4, you will have a better understanding of making network requests.

Should your passion be based on love? We mean true love…

Therefore, let us start identifying and understanding our love for Codeigniter web development through this tiny PHP cURL in the Codeigniter concept.

Before we jump on to making the PHP CURL request, we would like to familiarize you with some of the PHP curl methods that we are about to use in this quick post.

curl_setopt():
This little miniature method is notably helpful in invoking the predefined CURL session. The first parameter is about setting up the option for a cURL session, whereas the second parameter defines what option needs to be set.

curl_exec():
If you want to evoke the provided cURL session, rely on this method; it is triggered only after starting a cURL session.

curl_init():
It starts the new session and boldly calls the predefined CURL session.

Enough talk, let’s get to the point.

<?php 

namespace App\Controllers;
use CodeIgniter\Controller;


class cURLController extends Controller
{

    public function curPostRequest()
    {
        // API
        $api = 'http://www.localhost.com/api';
   
        // Set curl
        $cURL = curl_init($api);
   
        // Post data
        $data = [
            'name' => 'Roundness of Beauty', 
            'email' => 'jessi@yahoo.com',
            'profile' => 'Writer'
        ];
   
        // Execute CURL session
        curl_setopt($cURL, CURLOPT_POSTFIELDS, $data);
            
        // Set content type
        curl_setopt($cURL, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
            
        /* Return json */
        curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
            
        // Fire request
        $result = curl_exec($cURL);
             
        // Close curl
        curl_close($cURL);
    }
 
}

Here is how you can make the PHP cURL header authentication request using the provided code.

<?php 

namespace App\Controllers;

use CodeIgniter\Controller;


class cURLController extends Controller
{

    public function curPostRequest()
    {
        // API
        $api = 'http://www.localhost.com/api';
   
        // Set curl
        $cURL = curl_init($api);
   
        // Post data
        $data = [
            'name' => 'Roundness of Beauty', 
            'email' => 'jessi@yahoo.com',
            'profile' => 'Writer'
        ];
   
        // Execute CURL session
        curl_setopt($cURL, CURLOPT_POSTFIELDS, $data);
            
        // Set content type
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type:application/json',
            'App-Key: D55555999',
            'App-Secret: D5aCa6p'
        ));
            
        /* Return json */
        curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
            
        // Fire request
        $result = curl_exec($cURL);
             
        // Close curl
        curl_close($cURL);
    }
 
}

The tutorial has completed; we expect we have been used simple language with an easy example to fill your need.