Codeigniter 4 Send PHP cURL POST Request Example

Codeigniter 4 Send PHP, This tutorial will guide you through using PHP cURL to send a POST request in Codeigniter 4, including how to deal with headers and authentication. Sometimes, when fetching data from third-party endpoints in Codeigniter 4, you need to rely on a CURL request to get the data you need.

With PHP cURL functions, you can easily handle GET, POST, PUT, and DELETE requests in Codeigniter. It’s a comfortable and satisfying method for web developers.

We’ll provide an example of a PHP cURL request in Codeigniter 4, and we’ll also cover how to handle headers with authentication. You’ll also learn how to use PHP cURL functions to get the JSON response through an API.

PHP Codeigniter 4 cURL Request Example

In this tutorial, we will show you an example of a PHP cURL request in Codeigniter, including how to add headers to the request. You’ll also learn how to use PHP cURL functions to get a JSON response through an 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://yourserverpath.com/endpoint';
    
        /* eCurl */
        $curl = curl_init($url);
    
        /* Data */
        $data = [
            'name'=>'John Doe', 
            'email'=>'johndoe@yahoo.com'
        ];
    
        /* 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);
    }
}

Now, let’s take a look at an example of header authentication cURL in PHP CodeIgniter 4 app. Here’s how it works:

<?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://yourserverpath.com/endpoint';
    
        /* eCurl */
        $curl = curl_init($url);
    
        /* Data */
        $data = [
            'name'=>'John Doe', 
            'email'=>'johndoe@yahoo.com'
        ];
    
        /* 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);
    }
}

You May Also Like

  1. CodeIgniter 4 Server Side DataTable Example
  2. Codeigniter 4 Google Map Multiple Markers Example
  3. Codeigniter 4 Get Latitude and Longitude From Address
  4. Country State City Dependent Dropdown in Codeigniter 4
  5. Codeigniter 4 Import Data to Excel/CSV File From MySQL Database
  6. Codeigniter 4 Dynamic Dependent Dropdown with Ajax
  7. Codeigniter 4 Autocomplete Textbox From Database using Typeahead JS
  8. Codeigniter 4 PDF Generator Tutorial Example
  9. Crop and Save Image using jQuery Coppie in Codeigniter 4

Leave a Reply

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