Curl: Display the Web Page on terminal (stdout)
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." /> .....
Comments: