In this tutorial, I’ll give you brief description on using PHP cURL library or function in CodeIgniter. Now what is cURL and Why it is being used..??  cURL stands for ‘client URL’. In PHP cURL library let you make HTTP requests. PHP supports libcurl that permits to connect and communicate with different types of servers protocols.

cURL is used to get content from another websites through the applications. It is also used to post data to another website and download files via FTP or HTTP in short helps you manipulate the data. Hence,  it allows your application to act like a user accessing a website.


Download The Script Used For Data Retrieval Via Curl


Steps To Start Using cURL:

Step 1: Install the libcurl pakage.

In order to use PHP’s cURL functions you need to install the libcurl package. PHP requires that you use libcurl 7.0.2-beta or higher. Download the file and save that in libraries folder. Which you will find under system folder of your CodeIgniter base file.

Path : ci_demo > system > libraries > curl.php

Note:  You can make HTTP requests without cURL too, though it requires allow_url_fopen to be enabled in your php.ini file. 

Step 2: Now after saving curl.php file in library. Create a controller file controller_curl.php for calling curl.php and setting parameter to fetch server side information of a website.

Controller File : controller_curl.php


<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Controller_curl extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index(){

//  Calling cURL Library
$this->load->library('curl');

//  Setting URL To Fetch Data From
$this->curl->create('https://www.formget.com/');

//  To Temporarily Store Data Received From Server
$this->curl->option('buffersize', 10);

//  To support Different Browsers
$this->curl->option('useragent', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 (.NET CLR 3.5.30729)');

//  To Receive Data Returned From Server
$this->curl->option('returntransfer', 1);

//  To follow The URL Provided For Website
$this->curl->option('followlocation', 1);

//  To Retrieve Server Related Data
$this->curl->option('HEADER', true);

//  To Set Time For Process Timeout
$this->curl->option('connecttimeout', 600);

//  To Execute 'option' Array Into cURL Library & Store Returned Data Into $data
$data = $this->curl->execute();

//  To Display Returned Data
echo $data;
}
}
?>

Difference in code for HTTP and HTTPS Websites: 

There is a slight change in the code if we need to fetch data from SSL site. Here you need to set 3 SSL security Options.


<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$this->curl->create('https://www.formget.com/');
//  Rest Of The Code As In controller_curl.php
$this->curl->option('connecttimeout', 600);

// For SSL Sites. Check whether the Host Name you are connecting to is valid
$this->curl->option('SSL_VERIFYPEER', false);

//  Ensure that the server is the server you mean to be talking to
$this->curl->option('SSL_VERIFYHOST', false);

// Defines the SSL Version to be Used
$this->curl->option('SSLVERSION', 3);
$data = $this->curl->execute();
echo $data;
}
}
?>

Conclusion:

I hope that you have got an idea on how to use cURL library to fetch any website’s server side information. There are lot more things you can do with cURL, Keep reading our blog posts that will help you to enhance your knowledge and will sharpen your coding tricks. 🙂