cURL / Mailing Lists / curl-library / Single Mail

curl-library

Output to terminal using libcurl

From: Oscar Salvador <pyscholeber_at_gmail.com>
Date: Thu, 17 Mar 2011 13:51:31 +0100

Hi, how are you.
I have a doubt with libcurl. I'm retrieving an image from url, for
example: http://www.jejeje.com/pepe/52.jpg, and i save this in a
memory buffer. This works correctly.
Before retrieve the image, I check if image exists or no with:

[[ curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_code); ]]

If http_code contains "404", i close libcurl and return to he previous
function, but when i call curl_perform to see if image exists, i get
the output to the terminal.
Is there any solution for don't send to terminal?

The code is:

char *get_image(unsigned int id_photo)
{
        char url[64];
        CURL *curl_handle;
         struct MemoryStruct chunk;
        long http_code;

                chunk.memory = malloc(1);
                chunk.size = 0;

                snprintf( url, sizeof(url)-1,
"http://www.devicelost.com/caratulas/%d-x.jpg", id_photo);
                printf("URL-> [%s]\n", url);

                  curl_global_init(CURL_GLOBAL_ALL);

                    curl_handle = curl_easy_init();

                    curl_easy_setopt(curl_handle, CURLOPT_URL, url);

                curl_easy_perform(curl_handle);

                curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_code);
                
                if(http_code != HTTP_OK) {
                        free(chunk.memory);
                        curl_easy_cleanup(curl_handle);
                        curl_global_cleanup();
                        return NULL;
                }

                  curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writeinbuffer);

                  curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);

                   curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");

                  curl_easy_perform(curl_handle);

                  curl_easy_cleanup(curl_handle);

                FILE *file = fopen("image.jpg", "wb");
                fwrite(chunk.memory, 1, chunk.size, file);
                fclose(file);
                  curl_global_cleanup();
                  
                  return chunk.memory;
}

Thanks a lot,

Regards
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2011-03-17