How to Fire Curl Post Request using Codeigniter with Example

How to Fire Curl Post Request using Codeigniter with Example

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.

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'=&gt;'Hardik', 'email'=&gt;'[email protected]'];

    /* 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'=&gt;'Hardik', 'email'=&gt;'[email protected]'];

    /* 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...

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Learn More

PHP for Beginners - Become a PHP Master - CMS Project

Learn Object Oriented PHP By Building a Complete Website

PHP OOP: Object Oriented Programming for beginners + Project

Laravel PHP Framework Tutorial - Full Course for Beginners (2019)

Symfony PHP Framework - Full Course

What is a Full Stack Developer?

Build RESTful API In Laravel 5.8 Example

Laravel 5.8 Tutorial from Scratch for Beginners

Build a CRUD Operation using PHP & MongoBD

php codeigniter

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire PHP Developer

Looking to develop a PHP based website from scratch or revamp your existing website? **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")** has always been an industry leader for companies and business owners looking to hire...

Hire CodeIgniter Developer

Want to create unique, scalable web and app solutions? At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, Expert CodeIgniter developer works particularly for you and your project. You can **[Hire CodeIgniter...

Hire Dedicated PHP Developer

Looking to hire affordable yet experienced PHP developers? **[Hire Dedicated PHP Developer](https://hourlydeveloper.io/hire-dedicated-php-developer/ "Hire Dedicated PHP Developer")**, who can convert your idea to reality, within the stipulated...

Country State City Dropdown list in PHP MySQL PHP

Country state city dropdown using ajax in php. You'll learn how to populate country city state dropdown based on previous selection in php using ajax

PHP json_decode Example | Decode JSON Data in PHP Tutorial

PHP json_decode is an inbuilt function that takes the JSON encoded string and converts it into a PHP variable. JSON stands for JavaScript Object Notation.