2010-02-24 13 views
64

Desidero memorizzare il risultato di questa funzione di arricciatura in una variabile, come posso fare?C libcurl ottiene l'output in una stringa

#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, "curl.haxx.se"); 
    res = curl_easy_perform(curl); 

    /* always cleanup */ 
    curl_easy_cleanup(curl); 
    } 
    return 0; 
} 

grazie, ho risolto in questo modo:

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

function_pt(void *ptr, size_t size, size_t nmemb, void *stream){ 
    printf("%d", atoi(ptr)); 
} 

int main(void) 
{ 
    CURL *curl; 
    CURLcode res; 
    curl = curl_easy_init(); 
    if(curl) { 
    curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se"); 
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, function_pt); 
    curl_easy_perform(curl); 
    curl_easy_cleanup(curl); 
    } 
    system("pause"); 
    return 0; 
} 
+1

solo far notare nella vostra soluzione in function_pt() si sta convertendo la stringa nel PTR per intero al fine di riconvertirlo in stringa nell'output. È possibile emettere direttamente la stringa (e vedere la risposta completa). – zzz

+0

questo è un collegamento all'esempio cURL http://curl.haxx.se/libcurl/c/getinmemory.html – lafferc

+0

'Ris. Codice CURL 'non è utilizzato – fnc12

risposta

82

È possibile impostare una funzione di callback per ricevere blocchi di dati in entrata utilizzando curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, myfunc);

Il callback avrà un argomento definita dall'utente che è possibile impostare utilizzando curl_easy_setopt(curl, CURLOPT_WRITEDATA, p)

Ecco un frammento di codice che passa un buffer struct string {*ptr; len} a la funzione di callback e aumenta il buffer su ogni chiamata usando realloc().

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

struct string { 
    char *ptr; 
    size_t len; 
}; 

void init_string(struct string *s) { 
    s->len = 0; 
    s->ptr = malloc(s->len+1); 
    if (s->ptr == NULL) { 
    fprintf(stderr, "malloc() failed\n"); 
    exit(EXIT_FAILURE); 
    } 
    s->ptr[0] = '\0'; 
} 

size_t writefunc(void *ptr, size_t size, size_t nmemb, struct string *s) 
{ 
    size_t new_len = s->len + size*nmemb; 
    s->ptr = realloc(s->ptr, new_len+1); 
    if (s->ptr == NULL) { 
    fprintf(stderr, "realloc() failed\n"); 
    exit(EXIT_FAILURE); 
    } 
    memcpy(s->ptr+s->len, ptr, size*nmemb); 
    s->ptr[new_len] = '\0'; 
    s->len = new_len; 

    return size*nmemb; 
} 

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

    curl = curl_easy_init(); 
    if(curl) { 
    struct string s; 
    init_string(&s); 

    curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se"); 
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); 
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); 
    res = curl_easy_perform(curl); 

    printf("%s\n", s.ptr); 
    free(s.ptr); 

    /* always cleanup */ 
    curl_easy_cleanup(curl); 
    } 
    return 0; 
} 
+0

Risposta molto buona. Ho preso tutti quanti nella stringa. Yipee! –

+1

Bello. Ancora più bello se tutti quei 'size_t' (oltre a' len' stesso) sarebbero dichiarati 'const'. – alk

7

Dalla lettura del manuale qui: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html Penso che avete bisogno diverse chiamate al curl_setopt, il primo è l'URL che si desidera elaborare, il secondo essere qualcosa di simile:

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, function_ptr); 

Dove function_ptr corrisponde a questa firma:

size_t function(void *ptr, size_t size, size_t nmemb, void *stream) 

Quello che succede qui è la funzione di callback che libcurl chiamerà quando ha qualche output per scrivere da qualunque trasferimento tu abbia invocato. Puoi farlo scrivere automaticamente su un file o passarlo a una funzione che gestirà l'output stesso. Usando questa funzione dovresti essere in grado di assemblare le varie stringhe di output in un unico pezzo e poi usarle nel tuo programma.

Non sono sicuro di quali altre opzioni potresti dover impostare/che altro influisce sul modo in cui desideri che la tua app si comporti, quindi dai un'occhiata a quella pagina.

21

La risposta seguente è il modo C++ per farlo, con std::string, invece di stringa con terminazione null. Usa ancora una funzione di callback (non c'è modo di aggirarla), ma gestisce anche l'errore di allocazione usando try/catch.

#include <iostream> 
#include <string> 
#include <curl/curl.h> 

size_t CurlWrite_CallbackFunc_StdString(void *contents, size_t size, size_t nmemb, std::string *s) 
{ 
    size_t newLength = size*nmemb; 
    size_t oldLength = s->size(); 
    try 
    { 
     s->resize(oldLength + newLength); 
    } 
    catch(std::bad_alloc &e) 
    { 
     //handle memory problem 
     return 0; 
    } 

    std::copy((char*)contents,(char*)contents+newLength,s->begin()+oldLength); 
    return size*nmemb; 
} 
int main() 
{ 
    CURL *curl; 
    CURLcode res; 

    curl_global_init(CURL_GLOBAL_DEFAULT); 

    curl = curl_easy_init(); 
    std::string s; 
    if(curl) 
    { 

     curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se"); 

     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); //only for https 
     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); //only for https 
     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWrite_CallbackFunc_StdString); 
     curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); 
     curl_easy_setopt (curl, CURLOPT_VERBOSE, 1L); //remove this to disable verbose output 


     /* Perform the request, res will get the return code */ 
     res = curl_easy_perform(curl); 
     /* Check for errors */ 
     if(res != CURLE_OK) 
     { 
      fprintf(stderr, "curl_easy_perform() failed: %s\n", 
        curl_easy_strerror(res)); 
     } 

     /* always cleanup */ 
     curl_easy_cleanup(curl); 
    } 

    std::cout<<s<<std::endl; 

    std::cout<< "Program finished!" << std::endl; 
} 
+1

Ha funzionato bene per me. Facile e più breve delle risposte sopra ... !!! – mbaros

Problemi correlati