Codeigniter 4 Send PHP cURL POST Request Example

Codeigniter 4 Send PHP cURL POST Request Example

Sometimes, you need to fetch data from the third-party endpoint in Codeigniter 4, then you can’t rely upon more than a CURL request on any other method. So, In this tutorial, you will learn how to send or use php cURL post request in Codeigniter 4.

You can go for php curl functions to deal with GET, POST, PUT, DELTE requests in Codeigniter with PHP CURL functions. It’s not just comfortable but also sole satisfying for web developers

And will show you a PHP cURL request example in codeigniter 4 app, along with that also help you deal with headers with authentication. And PHP curl functions to get the JSON response through API.

How to Use PHP cURL POST Request in Codeigniter

In this tutorial, you will learn about PHP codeigniter cURL request example with the header. And PHP curl functions to get the JSON response through API.

And you can see the following functions of PHP Codeigniter 4 cURL request:

  • curl_setopt(): This method sets an option for a cURL transfer
  • curl_init(): Initialize a new session and return a cURL handle for use with the curl_setopt(), curl_exec(), and curl_close() functions.
  • curl_exec(): It is recommended to call this method after evoking a cURL session along with every set session. option.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class CurlController extends CI_Controller {

    public function __construct() {
       parent::__construct();
    }
   
    public function curPostRequest()
    {
        /* Endpoint */
        $url = 'http://www.localhost.com/endpoint';
   
        /* eCurl */
        $curl = curl_init($url);
   
        /* Data */
        $data = [
            'name'=>'John Doe', 
            'email'=>'[email protected]'
        ];
   
        /* Set JSON data to POST */
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            
        /* Define content type */
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
            
        /* Return json */
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            
        /* make request */
        $result = curl_exec($curl);
             
        /* close curl */
        curl_close($curl);
    }
}

Let us check out the header authentication cURL in PHP CodeIgniter 4 app with the following example:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class CurlController extends CI_Controller {

    public function __construct() {
       parent::__construct();
    }
   
    public function curPostRequest()
    {
        /* Endpoint */
        $url = 'http://www.localhost.com/endpoint';
   
        /* eCurl */
        $curl = curl_init($url);
   
        /* Data */
        $data = [
            'name'=>'John Doe', 
            'email'=>'[email protected]'
        ];
   
        /* Set JSON data to POST */
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            
        /* Define content type */
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type:application/json',
            'App-Key: JJEK8L4',
            'App-Secret: 2zqAzq6'
        ));
            
        /* Return json */
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            
        /* make request */
        $result = curl_exec($curl);
             
        /* close curl */
        curl_close($curl);
    }
}

Recommended Codeingiter Tutorials

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *