linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* libCurl in C
@ 2009-01-20 20:43 Jai Sharma
  2009-01-20 20:52 ` Fabian Ischia
  2009-01-20 21:10 ` Jose Celestino
  0 siblings, 2 replies; 3+ messages in thread
From: Jai Sharma @ 2009-01-20 20:43 UTC (permalink / raw)
  To: linux-c-programming

Dear Friends,

I am using CURL to get HTTP response.
The default output for CURL is stdout, but i am unable to change it to
a variable.

Right now,
I am using a temporary file for this purpose and read it to process it.
Is there any way, by which method i will get CURL output to a string
or any structure?

===================== main.c =====================

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

int
main()
{
	CURL *curl;
	char *data1[512];

	FILE *fp;
	fp = fopen("/tmp/curl","w");

	curl = curl_easy_init();
  	if(curl) {
    		curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1");
    		curl_easy_setopt(curl, CURLOPT_HEADER, 0);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
	    	curl_easy_perform(curl);
	    	curl_easy_cleanup(curl);
  	}

	fclose(fp);    	
	return 0;
}

================================================

Thanks and Regards
Jai

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: libCurl in C
  2009-01-20 20:43 libCurl in C Jai Sharma
@ 2009-01-20 20:52 ` Fabian Ischia
  2009-01-20 21:10 ` Jose Celestino
  1 sibling, 0 replies; 3+ messages in thread
From: Fabian Ischia @ 2009-01-20 20:52 UTC (permalink / raw)
  To: Jai Sharma; +Cc: linux-c-programming

You have to set a write function that will be called when curl receives 
the data. You can do whatever you want with the raw info retrieved by 
the funciton

........
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_data);
..........


size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp)
{
    char *data;

    fprintf(stderr, "Allocating %ld * %ld = %ld\n", (long)size, 
(long)nmemb, (long)(size * nmemb));
    data = calloc(size, nmemb);
    if(!data)
        return -1;
    fprintf(stderr, "Allocation succeeded\n");

    memcpy(data, buffer, size * nmemb);

    return size * nmemb;
}


Jai Sharma wrote:
> Dear Friends,
>
> I am using CURL to get HTTP response.
> The default output for CURL is stdout, but i am unable to change it to
> a variable.
>
> Right now,
> I am using a temporary file for this purpose and read it to process it.
> Is there any way, by which method i will get CURL output to a string
> or any structure?
>
> ===================== main.c =====================
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <curl/curl.h>
>
> int
> main()
> {
> 	CURL *curl;
> 	char *data1[512];
>
> 	FILE *fp;
> 	fp = fopen("/tmp/curl","w");
>
> 	curl = curl_easy_init();
>   	if(curl) {
>     		curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1");
>     		curl_easy_setopt(curl, CURLOPT_HEADER, 0);
> 		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
> 	    	curl_easy_perform(curl);
> 	    	curl_easy_cleanup(curl);
>   	}
>
> 	fclose(fp);    	
> 	return 0;
> }
>
> ================================================
>
> Thanks and Regards
> Jai
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
>   

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: libCurl in C
  2009-01-20 20:43 libCurl in C Jai Sharma
  2009-01-20 20:52 ` Fabian Ischia
@ 2009-01-20 21:10 ` Jose Celestino
  1 sibling, 0 replies; 3+ messages in thread
From: Jose Celestino @ 2009-01-20 21:10 UTC (permalink / raw)
  To: Jai Sharma; +Cc: linux-c-programming

Words by Jai Sharma [Wed, Jan 21, 2009 at 02:13:04AM +0530]:
> Dear Friends,
> 
> I am using CURL to get HTTP response.
> The default output for CURL is stdout, but i am unable to change it to
> a variable.
> 
> Right now,
> I am using a temporary file for this purpose and read it to process it.
> Is there any way, by which method i will get CURL output to a string
> or any structure?
> 

I usually do:

/* store curl "output" here */
char buffer[BUFFER_SIZE];

/* function that does the copy */
size_t tobuffer(char *ptr, size_t size, size_t nmemb, void *stream)
{
	strncpy(buffer,ptr,size*nmemb);
	return size*nmemb;
}

....
/* tell curl to use the upper function */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &tobuffer);
....

then do whatever I want (print, parse, etc) with buffer.

-- 
Jose Celestino | http://japc.uncovering.org/files/japc-pgpkey.asc
----------------------------------------------------------------
"One man’s theology is another man’s belly laugh." -- Robert A. Heinlein
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-01-20 21:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-01-20 20:43 libCurl in C Jai Sharma
2009-01-20 20:52 ` Fabian Ischia
2009-01-20 21:10 ` Jose Celestino

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).