In this tutorial you will learn about the Codeigniter 4 cURL POST Request Example Tutorial and its application with practical example.
In this Codeigniter 4 cURL POST Request Example Tutorial I’ll show you how to post form data using curl post request method in codeigniter. In this tutorial you will learn to make curl POST request in codeigniter. You will also learn to make curl request with header authentication in codeigniter. In this article I will share example to how to use and make curl request in codeigniter.
PHP Codeigniter cURL POST Request example
In this example we will be implementing codeigniter controller method to make curl POST request. We will be setting curl url and curl request parameters for POST request in controller method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class CurlController extends CI_Controller { public function __construct() { parent::__construct(); } public function curlPostRequest() { /* Endpoint */ $url = 'http://www.localhost.com/endpoint'; /* eCurl */ $curl = curl_init($url); /* Set POST data */ curl_setopt($curl, CURLOPT_POST, 1); /* Data */ $data = [ 'name'=>'John Doe', 'email'=>'johndoe@gmail.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); } } ?> |
PHP Codeigniter cURL POST Request with header authentication
In this example we will be implementing codeigniter controller method to make curl POST request with header authentication. We will be setting curl url and curl request parameters for POST request in controller method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class CurlController extends CI_Controller { public function __construct() { parent::__construct(); } public function curlPostRequest() { /* Endpoint */ $url = 'http://www.localhost.com/endpoint'; /* eCurl */ $curl = curl_init($url); /* Set POST data */ curl_setopt($curl, CURLOPT_POST, 1); /* Data */ $data = [ 'name'=>'John Doe', 'email'=>'johndoe@gmail.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); } } ?> |