Curl: Display the Web Page on terminal (stdout)

Posted by Joys of Programming on in Curl, Linux, Web

curl can be used to transfer to and fro from a server. You can download a webpage using curl. But if you want to display the contents of a web page on to the terminal or standard output, all you need is not to set the option CURLOPT_WRITEDATA which is used to specify the FILE * stream, after setting the CURLOPT_WRITEFUNCTION parameter.  By not specifying the FILE * stream, curl will use stdout to write the contents of the file.


#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
#define WEBPAGE_URL "http://wikipedia.org"

size_t write_data( void *ptr, size_t size, size_t nmeb, void *stream)
{
 return fwrite(ptr,size,nmeb,stream);
}

int main()
{
 CURL *handle = curl_easy_init();
 curl_easy_setopt(handle,CURLOPT_URL,WEBPAGE_URL); /*Using the http protocol*/
 curl_easy_setopt(handle,CURLOPT_WRITEFUNCTION, write_data);
 curl_easy_perform(handle);
 curl_easy_cleanup(handle);
}

After compiling and executing the program, you will get the following output.


<!doctype html>

<!-- Sysops: Please do not edit the main template directly; update /temp and synchronise. -->

<html lang="mul" dir="ltr">
<head>
<meta charset="utf-8" />
<title>Wikipedia</title>
<meta name="title" content="Wikipedia" />
<meta name="description" content="Wikipedia, the free encyclopedia that anyone can edit." />
.....


Tags: , , , ,

Comments:

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>

Copyright © 2009-2012 Joys of Programming All rights reserved.
Desk Mess Mirrored v1.8.1 theme from BuyNowShop.com.