ItSolutionStuff.com

Codeigniter Curl Post Request with Parameters Example

By Hardik Savani • November 5, 2023
PHP Codeigniter

In this tutorial, i will give you example of how to fire curl post request with codeigniter. it will help to get third party api data using curl request in codeigniter. you can fire post request, get request, put request and delete request in curl codeigniter 3.

I will suggest to use code php curl function like curl_init(), curl_setopt(), curl_exec() etc. using cURL we will call apis to getting json data and we can use their data in our project. i don't think you should use another library for this.

Sometime we need to work with web services and APIs of third party website, at that time we need to use php curl for get request, post request, delete request, put request ect. php curl will help to post request with parameters and headers, we can get json response.

Here, i will give you very simple example of curl request and also give you headers with authentication example on bellow of simple curl request example:

Simple Example:

<?php

class MyClass extends CI_Controller {

/**

* Index Page for this controller.

*

*/

public function simleExample()

{

/* API URL */

$url = 'http://www.mysite.com/api';

/* Init cURL resource */

$ch = curl_init($url);

/* Array Parameter Data */

$data = ['name'=>'Hardik', 'email'=>'itsolutionstuff@gmail.com'];

/* pass encoded JSON string to the POST fields */

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

/* set the content type json */

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

/* set return type json */

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

/* execute request */

$result = curl_exec($ch);

/* close cURL resource */

curl_close($ch);

}

}

Header Auth Example:

<?php

class MyClass extends CI_Controller {

/**

* Index Page for this controller.

*

*/

public function simleExample()

{

/* API URL */

$url = 'http://www.mysite.com/api';

/* Init cURL resource */

$ch = curl_init($url);

/* Array Parameter Data */

$data = ['name'=>'Hardik', 'email'=>'itsolutionstuff@gmail.com'];

/* pass encoded JSON string to the POST fields */

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

/* set the content type json */

curl_setopt($ch, CURLOPT_HTTPHEADER, array(

'Content-Type:application/json',

'App-Key: 123456',

'App-Secret: 1233'

));

/* set return type json */

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

/* execute request */

$result = curl_exec($ch);

/* close cURL resource */

curl_close($ch);

}

}

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Codeigniter Form Validation with Error Message

Read Now →

Codeigniter Add/Remove Multiple Input Fields Dynamically using JQuery

Read Now →

Codeigniter Compress Image Size Example

Read Now →

Codeigniter Stripe Payment Gateway Integration Example

Read Now →

Codeigniter Ajax Pagination using JQuery Example

Read Now →

Codeigniter Delete Multiple Rows using Checkbox Example

Read Now →

Codeigniter JQuery Ajax Request Example

Read Now →

Codeigniter Multiple Database Connection Example

Read Now →

Codeigniter Resize Image and Create Thumbnail Example

Read Now →

Laravel CURL Request Example using Ixudra/curl

Read Now →

Codeigniter Drag and Drop Multiple Image Upload Example

Read Now →

Codeigniter Ajax CRUD Tutorial Example

Read Now →

Codeigniter Select2 Ajax Autocomplete from Database Example

Read Now →

PHP Download File from URL using CURL Request Example

Read Now →