libcurl is a famous C library which can be used to transfer data through http/tcp or any custom protocols. It has a very easy to use API to make web requests programatically.

PHP has an extension that wraps the libcurl API and provide a very convenient API to PHP programmers.

Normally PHP has a lot of functions that are available in C standard libraries. For an example you have the strlen in both C and PHP that gives you the length of a string. But PHP has additional helper functions that allows programmers to manipulate strings easily. For an example you can compare strings in PHP using strcmp function as in C style. Or you can simply use “==” operator.

Similarly libcurl APi for PHP  is very similar to the C API. But it has specially made to suit for the PHP language.

Here is a C code that make a web request to my blog and print it in the console.

#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include <string.h>

/* function prototypes to define later */
char *do_web_request(char *url);
size_t static write_callback_func(void *buffer,
                        size_t size,
                        size_t nmemb,
                        void *userp);

/* the main function invoking */
int main()
{
    char *url = "http://dimuthu.org";
    char *content = NULL;

    content = do_web_request(url);

    printf("%s", content);
}

/* the function to return the content for a url */
char *do_web_request(char *url)
{
    /* keeps the handle to the curl object */
    CURL *curl_handle = NULL;
    /* to keep the response */
    char *response = NULL;

    /* initializing curl and setting the url */
    curl_handle = curl_easy_init();
    curl_easy_setopt(curl_handle, CURLOPT_URL, url);
    curl_easy_setopt(curl_handle, CURLOPT_HTTPGET, 1);

    /* follow locations specified by the response header */
    curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);

    /* setting a callback function to return the data */
    curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_callback_func);

    /* passing the pointer to the response as the callback parameter */
    curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &response);

    /* perform the request */
    curl_easy_perform(curl_handle);

    /* cleaning all curl stuff */
    curl_easy_cleanup(curl_handle);

    return response;
}

/* the function to invoke as the data recieved */
size_t static write_callback_func(void *buffer,
                        size_t size,
                        size_t nmemb,
                        void *userp)
{
    char **response_ptr =  (char**)userp;

    /* assuming the response is a string */
    *response_ptr = strndup(buffer, (size_t)(size *nmemb));

}

Lets see how simple it is with PHP.

<?php

echo do_web_request("http://dimuthu.org/");

/* the function to make the request */
function do_web_request($url)
{
    /* initializing curl */
    $curl_handle = curl_init($url);

    /* set this option the curl_exec function return the response */
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);

    /* follow the redirection */
    curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, 1);

    /* invoke the request */
    $response = curl_exec($curl_handle);

    /* cleanup curl stuff */
    curl_close($curl_handle);

    return $response;
}

?>