Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

przykład do biblioteki sieciowej

Ostatnio zmodyfikowano 2009-03-29 13:14
Autor Wiadomość
pixelmaster
» 2009-03-24 20:53:15
Znalazłem coś takiego http://www.luckyspin.org/?p=28 ale nie sprawdzałem czy działa.
Wkleje kod z tamtąd, aby mój post wyglądał pokaźniej :E
C/C++
/*
* This is a very simple example of how to use libcurl from within
* a C++  program. The basic idea is that you want to retrieve the
* contents of a web page as a string. Obviously, you can replace
* the buffer object with anything you want and adjust elsewhere
* accordingly.
*
* Hope you find it useful..
*
* Todd Papaioannou
*/

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

using namespace std;

// Write any errors in here 
static char errorBuffer[ CURL_ERROR_SIZE ];

// Write all expected data in here 
static string buffer;

// This is the writer call back function used by curl 
static int writer( char * data, size_t size, size_t nmemb,
std::string * buffer )
{
    // What we will return 
    int result = 0;
   
    // Is there anything in the buffer? 
    if( buffer != NULL )
    {
        // Append the data to the buffer 
        buffer->append( data, size * nmemb );
       
        // How much did we write? 
        result = size * nmemb;
    }
   
    return result;
}

// You know what this does.. 
void usage()
{
    cout < < "curltest: \n" << endl;
    cout << "  Usage:  curltest url\n" << endl;
}

/*
* The old favorite
*/
int main( int argc, char * argv[] )
{
    if( argc > 1 )
    {
        string url( argv[ 1 ] );
       
        cout < < "Retrieving " << url << endl;
       
        // Our curl objects 
        CURL * curl;
        CURLcode result;
       
        // Create our curl handle 
        curl = curl_easy_init();
       
        if( curl )
        {
            // Now set up all of the curl options 
            curl_easy_setopt( curl, CURLOPT_ERRORBUFFER, errorBuffer );
            curl_easy_setopt( curl, CURLOPT_URL, argv[ 1 ] );
            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 );
           
            // Attempt to retrieve the remote page 
            result = curl_easy_perform( curl );
           
            // Always cleanup 
            curl_easy_cleanup( curl );
           
            // Did we succeed? 
            if( result == CURLE_OK )
            {
                cout << buffer << "\n";
                exit( 0 );
            }
            else
            {
                cout << "Error: [" << result << "] - " << errorBuffer;
                exit( - 1 );
            }
        }
    }
}

P-4997
pekfos
Temat założony przez niniejszego użytkownika
» 2009-03-25 12:04:49
przetłumaczę sobie komentarze to może coś zrozumiem..

C/C++
#include <string> 
#include <iostream> 
#include "curl/curl.h" 

using namespace std;

// Write any errors in here
//: wypisuje tutaj każdy błąd

static char errorBuffer[ CURL_ERROR_SIZE ];

// Write all expected data in here 
//:wypisuje tutaj wszystkie przewidywane dane
static string buffer;

// This is the writer call back function used by curl
// hmm.. nie wiem.. 
static int writer( char * data, size_t size, size_t nmemb,
std::string * buffer )
{
    // What we will return
    //:co zwrócimy?
    int result = 0;
   
    // Is there anything in the buffer?
    //:czy jest cokolwiek w buforze? 
    if( buffer != NULL )
    {
        // Append the data to the buffer
        //nie wiem. 
        buffer->append( data, size * nmemb );
       
        // How much did we write?
        //:jak dużo napiszemy? 
        result = size * nmemb;
    }
   
    return result;
}

// You know what this does..
//:wiesz co to robi..
void usage()
{
    cout < < "curltest: \n" << endl;
    cout << "  Usage:  curltest url\n" << endl;
}

/*
* The old favorite
*/
int main( int argc, char * argv[] )
{
    if( argc > 1 )
    {
        string url( argv[ 1 ] );
       
        cout < < "Retrieving " << url << endl;
       
        // Our curl objects 
        //:nasze obiekty curl'a
        CURL * curl;
        CURLcode result;
       
        // Create our curl handle
        //:tworzy nasz uchwyt curl'a 
        curl = curl_easy_init();
       
        if( curl )
        {
            // Now set up all of the curl options 
            //:teraz ustawia wszystkie opcje curl'a
            curl_easy_setopt( curl, CURLOPT_ERRORBUFFER, errorBuffer );
            curl_easy_setopt( curl, CURLOPT_URL, argv[ 1 ] );
            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 );
           
            // Attempt to retrieve the remote page
            //nie mam pojęcia..
            result = curl_easy_perform( curl );
           
            // Always cleanup
            //:zawsze czyści??
            curl_easy_cleanup( curl );
           
            // Did we succeed?
            //:udało sie?
           
            if( result == CURLE_OK )
            {
                cout << buffer << "\n";
                exit( 0 );
            }
            else
            {
                cout << "Error: [" << result << "] - " << errorBuffer;
                exit( - 1 );
            }
        }
    }
}

tego curl_easy_setopt() nie rozumiem..
wiem tylko że pierwszy argument to wskaźnik do objektu curl'a
P-4999
DejaVu
» 2009-03-25 12:34:48
curl_easy_setopt()

Innymi słowy: Curl Easy Set Option
Parametr 1: wskaźnik do zainicjowanej struktury
Parametr 2: opcja, którą chcemy ustawić
Parametr 3: nowa wartość dla opcji
P-5000
pekfos
Temat założony przez niniejszego użytkownika
» 2009-03-25 12:39:04
jakie opcje trzeba ustawić?

//edit:

CURLOPT_URL domyślam sie że adres strony internetowej
CURLOPT_ERRORBUFFER bufor błędów

tylko jeszcze nie rozumiem dwóch opcji:
CURLOPT_HEADER i CURLOPT_FOLLOWLOCATION.

//edit2:

CURLOPT_WRITEDATA wskaźnik do bufora do którego chyba
                         będą zapisywane dane
CURLOPT_WRITEFUNCTION wskaźnik do jakiejś funkcji
                         (jeszcze tego nie rozpracowałem:D)

//edit3:

hmm.. nie rozumiem co robi fukcja writer() w tym przykładzie

//edit4:

czy na repo jest paczka curl'a bo coś niewiem którą
trzeba pobrać (mnóstwo tego :D)
P-5002
DejaVu
» 2009-03-25 18:30:22
Jeśli używasz Dev-C++, skorzystaj z aktualizacji - pójdzie dużo łatwiej i niemalże bezboleśnie.
P-5012
pekfos
Temat założony przez niniejszego użytkownika
» 2009-03-25 18:54:02
7.14.0_ssl czy 7.14.0_nossl??
P-5015
pekfos
Temat założony przez niniejszego użytkownika
» 2009-03-28 09:08:49
hm.. są dwie wersje curl'a i nie wiem którą wybrać i nie
wiem czym się różnią.
7.14.0_ssl i 7.14.0_nossl
którą wybrać?
P-5072
DejaVu
» 2009-03-28 10:19:22
Jedna obsługuje SSL druga nie.
P-5075
1 « 2 » 3
Poprzednia strona Strona 2 z 3 Następna strona