Codeigniter 4 Send PHP cURL POST Request Example

In this tutorial, we will show you how to send a PHP cURL POST request in Codeigniter 4.

First, you need to install the Codeigniter 4 application using the composer command.

composer create-project codeigniter4/appstarter ci4

After that, open the ci4 project in any text editor.

Now, create a controller file PostRequest.php in the app/Controllers folder and add the following code in it.

‘John Doe’, ’email’ => ‘[email protected]’); $options = array( CURLOPT_URL => $url, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $data, CURLOPT_RETURNTRANSFER => true ); // Execute cURL request $ch = curl_init(); curl_setopt_array($ch, $options); $response = curl_exec($ch); curl_close($ch); // Print response echo $response; } }

Now, open the routes.php file in the app/Config folder and add the following route.

$routes->get(‘post-request’, ‘PostRequest::index’);

Now, open the browser and hit the following URL.

http://localhost/post-request

It will send a POST request to the http://example.com/api/post URL and print the response.

Codeigniter 4 curl post request with headers example. In this tutorial, you will learn how to send php cURL post request in codeigniter 4.

And this tutorial will explain how to use PHP Curl POST in Codeigniter 4. Sometimes, you need to fetching data from the third-party endpoint in Codeigniter 4, then you can’t rely upon more than a CURL request on any other method.

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.

PHP Codeigniter 4 cURL Request Example

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

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?