Making Web Requests Using Curl From C and PHP

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;
}

?>
This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

4 Responses to Making Web Requests Using Curl From C and PHP

  1. Andrew K says:

    Thanks for posting this! I’m working on my own Class in C++ for downloading web pages and saving them in a database. This is a great explanation of CURLOPT_WRITEFUNCTION and CURLOPT_WRITEDATA :)

  2. dimuthu says:

    Nice to know this was helpful:)

  3. M Sanda says:

    hi, this is very helpful
    i am new to c/c++ and i try to figure something out
    i want to login to a page and then check if it contains a word like this
    if (strstr(content, “IMPORTANT”)) puts(“Evrika\n”);
    the thing is that i don’t receive the entire page, only a part of it in the output
    seems like the buffer is overloaded after login to the website and on the final page request, i get only first part of it
    do you have any idea how to clear the buffer? ( if thats the problem )
    tx a lot

  4. Jennifer says:

    Where can I download the curl includes?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>