Curl - zapis do zmiennej

0

Witam. Jestem nowy w c++ a chciał bym napisać prosty programik. Chce pobrać stronę do zmiennej. Czytałem troszkę w necie o tym ale nie potrafię zaadaptować tego do mojego programu. Wytłumaczy mi ktoś jak to zrobić?? Na razie wyświetla mi pobraną treść a nie chcę jej wyświetlać tylko pobrać ja do zmiennej,

Oto kod:

    CURL *curl;
    CURLcode res;
    int i=0;
    char linkc[100];
    while(i<100)
    {
        linkc[i] = url[i];  
        i++; 
    }
  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, linkc );
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
  }
0

Zajrzyj do dokumentacji curla. Potrzebujesz napisać funkcję zwrotną(callback).

1
#include <iostream>
#include <string>  
#include "curl/curl.h" 
using namespace std;  

static char errorBuffer[CURL_ERROR_SIZE]; 
static string buffer; 
static int writer(char *data, size_t size, size_t nmemb, string *buffer)  
{ 
  int result = 0;    
  if (buffer != NULL)  
  { 
    buffer->append(data, size * nmemb); 
    result = size * nmemb;  
  } 
  return result;  
} 


int main()  
{
      CURL *curl; 
      CURLcode result;   
      struct curl_slist *headers=NULL;
      headers = curl_slist_append(headers, "Content-Type: text/html");  
      curl = curl_easy_init();  
      if (curl)  
      { 
         curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);  
         curl_easy_setopt(curl, CURLOPT_URL,"http://google.pl");  
         curl_easy_setopt(curl, CURLOPT_HEADER, 0);  
         curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);  
         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);  
         curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);  
      
         result = curl_easy_perform(curl);       
         curl_easy_cleanup(curl);  
         if (result == CURLE_OK)   
         { 
           cout<<buffer;
         }
         else html="none";
      }
      cout<<buffer;  //buffer zawiera zawartość strony :)
      return 0;
}
0

Witam,
Mam podobny problem tylko musze funkcje napisac w czystym C jak na razie wyświetla mi tylko <null>null>

static char errorBuffer[256]; 
static char *buffer; 
static int writer(char *data, size_t size, size_t nmemb, char *buffer)  
{ 
  int result = 0;    
  if (buffer != NULL)  
  { 
    strncmp(buffer, data, size * nmemb); 
    result = size * nmemb;  
  } else {
	  buffer = (char*) malloc(sizeof(char)* size * nmemb);
	  strncpy(buffer, data, size * nmemb); 
	  result = size * nmemb; 
  }
  return result;  
} 


int getSite()  
{
      CURL *curl; 
      CURLcode result;   
      struct curl_slist *headers=NULL;
      headers = curl_slist_append(headers, "Content-Type: text/html");  
      curl = curl_easy_init();  
      if (curl)  
      { 
         curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);  
         curl_easy_setopt(curl, CURLOPT_URL,"http://google.pl");  
         curl_easy_setopt(curl, CURLOPT_HEADER, 0);  
         curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);  
         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);  
         curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);  
      
         result = curl_easy_perform(curl);       
         curl_easy_cleanup(curl);  
         if (result == CURLE_OK)   
         { 
           printf("%s", buffer);
         }
         else return 1;
      }
      printf("%s", buffer);  //buffer zawiera zawartość strony :)
      return 0;
}
0

Jako że dokumentacja wg mnie jest naprawdę bardzo kiepska i wyjątkowo nieprzejrzysta, a google są jak damska torebka, pomyślałem że wrzucę tutaj funkcję zapisującą do pliku strony z CURL:

static size_t write_data_toFile(void *ptr, size_t size, size_t nmemb, void *stream) {
  size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
  return written;
}

void stronaDoPliku(char * adres, char * nazwa_pliku)  {
    
    static const char *pagefilename = nazwa_pliku;
    FILE *pagefile;
    
    CURL *curl_handle;
    curl_global_init(CURL_GLOBAL_ALL);

    curl_handle = curl_easy_init(); /* init the curl session */

    curl_easy_setopt(curl_handle, CURLOPT_URL, adres); /* set URL to get here */

    curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data_toFile);  /* send all data to this function  */

    pagefile = fopen(pagefilename, "wb"); /* open the file */
    if (pagefile) {

      curl_easy_setopt(curl_handle, CURLOPT_FILE, pagefile); /* write the page body to this file handle. CURLOPT_FILE is also known as
	CURLOPT_WRITEDATA*/
      
      curl_easy_perform(curl_handle); /* get it! */
      
      printf("Pobrano strone: %s do pliku: %s\n", adres, nazwa_pliku);
      
      fclose(pagefile); /* close the header file */
    }

    curl_easy_cleanup(curl_handle);	/* cleanup curl stuff */
}

int main(void) {
  char adres[] = "http://curl.haxx.se/libcurl/c/allfuncs.html";
  char nazwa_pliku[] = "strona.html";
  stronaDoPliku( adres, nazwa_pliku);
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1