April 13, 2008 by Alex Polski

Curl library for CodeIgniter

So, let’s continue to develop Mass PageRank Checker tool. I will use CodeIgniter (CI) PHP framework for it. It’s really simple and very powerful thing. But first I need Curl library for CI to make easier working with HTTP queries.

In brief, the library must do the following things:

  1. Send HTTP GET/POST requests and receive HTTP responses.
  2. Get information about queries like HTTP error code, total time for operation, number of sent/received bytes for operation, etc.
  3. Handle the errors.

I just wrote this library, you can download it here.

To use it just copy Curl.php to CI’s system/application/libraries folder, curl_lang.php to CI’s system/application/language/english folder and load it from your controllers:

$this->load->library('curl');

For example let’s simulate the search in Google:

$keyword = 'travel';
$this->load->library('curl');
$this->curl->open();
$content = $this->curl->http_get("http://www.google.com/search?q=$keyword");
$this->curl->close();
echo $content;

This is very simple example. At this moment the library has not powerful features. Here the list of what you can do and how you can do using Curl library:

  • Set additional options like User-Agent (MSIE 7.0 by default), timeout for operations in seconds (30 by default).
$this->curl->open(array('user_agent' => 'CURL library', 'timeout' => 60));
  • Send HTTP GET query and get the content. Setting $headers_only = TRUE will cause you receive HTTP headers only. You can specify additional HTTP headers in $headers variable.
function http_get($url, $headers = array(), $headers_only = FALSE)
  • Send HTTP POST query and get the content. POST parameters must be specified in $fields variable like ‘a=1&b=2&str=test%20example’
function http_post($url, $fields, $headers = array(), $headers_only = FALSE)
  • There are a few functions for getting different information about query
function get_http_code()
function get_total_time()
function get_bytes_uploaded()
function get_bytes_downloaded()
function get_speed_upload()
function get_speed_download()

In the future I’m planning to add the following features to library: referer and autoreferer support, cookies, proxies, authorization, follow location feature, caching, file uploading, additional headers and may be some more.

Share and Enjoy:
  • del.icio.us
  • Digg
  • Reddit
  • Ma.gnolia
  • Technorati
  • Propeller
  • Facebook
  • StumbleUpon
  • Furl
  • blogmarks
  • Google
  • E-mail this story to a friend!
This entry was posted on Sunday, April 13, 2008 at 7:50 am and is filed under CodeIgniter, Curl Library. You can leave a response, or trackback from your own site.

Related posts

« New blog’s design and Blueprint CSS framework

How to check Google PageRank in PHP »



3 Responses to “Curl library for CodeIgniter”

UALANCER
Posted on April 14th, 2008 at 1:11 pm

Wow. Great work, Alex! Thx

SEO бриз » Blog Archive » CURL Library для Code Igniter
Posted on April 18th, 2008 at 1:13 pm

[...] краток. Собственнно, вот он: http://alexpolski.com/2008/04/13/curl-library-for-codeigniter/ Пример [...]

links for 2008-05-23 | Digitalistic - Mashup or die trying
Posted on May 23rd, 2008 at 8:35 am

[...] Curl library for CodeIgniter | Alex Polski’s Blog Curl library for CI to make easier working with HTTP queries. (tags: curl codeigniter library php) [...]

Leave a Reply