Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read data from the web 1

Status
Not open for further replies.

smoker616

Programmer
May 2, 2003
14
0
0
US
I am running Mandrake linux and I have libcurl2-7.10.3 installed on it. I want to use the curl functions to read a html file from the web into a string.

I currently have this

#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
CURL *curl;
CURLcode res;

curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, &quot; res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
return 0;
}

This gets output from and prints it to the screen. I need to have it output to a string which i can then parse.

I dont know Curl at all but I only need it for this very simple function. If anyone knows a better way to read a page from the web from a C program please let me know

I had previously written this in php but i need more speed

in php it looked somthing like this

$fp = fopen(&quot;
and then i could just read $fp as if it were a local file

Thanks for any help you can give me
 
Here's a basic program that uses Curl and reads the body into a buffer. Hope it helps.

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#include <curl/curl.h>

static unsigned char *body;
static size_t body_size;
static size_t body_capacity;

#define INIT_BODY_SIZE 1024

size_t do_body(void *ptr, size_t size, size_t nmemb, void *stream)
{
while (body_size + nmemb > body_capacity) {
char *tmp = realloc(body, body_capacity * 2 + 1);
assert(tmp != NULL);
body = tmp;
body_capacity *= 2;
}
memcpy(body + body_size, ptr, nmemb);
body_size += nmemb;

return nmemb;
}

size_t do_header(void *ptr,size_t size,size_t nmemb,void *stream)
{
char *line;

/* You should at least examine the status line here to verify you
* got an HTTP OK. */

line = alloca(nmemb + 1);
assert(line != NULL);
memcpy(line, ptr, nmemb);
line[nmemb] = '\0';
printf(&quot;Header Line = %s\n&quot;, line);
return nmemb;
}

int main(int argc, char *argv[])
{
CURL *curl;

if (argc < 2) {
fprintf(stderr, &quot;usage: %s <url>\n&quot;, argv[0]);
return EXIT_FAILURE;
}

assert((body = malloc(INIT_BODY_SIZE + 1)) != NULL);
body_capacity = INIT_BODY_SIZE;

curl = curl_easy_init();
if (curl == NULL) {
fputs(&quot;Curl initialization failed\n&quot;, stderr);
} else {
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
/* Set function for processing body. */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, do_body);
/* Set function for processing header. */
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, do_header);
res = curl_easy_perform(curl);
if (body_size > 0 && res == CURLE_OK) {
/* Zero-terminate to handle as string */
*(body + body_size) = '\0';

/* Now the body of the resource is stored in 'body' */

free(body);
}
curl_easy_cleanup(curl);
}
return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top