cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: How can i get the answer from a post ?

From: Jabka Atu <mashrom.head_at_gmail.com>
Date: Sun, 27 Jul 2008 23:12:04 +0300

I can't figure what im doing wrong ,
After posting that form i should get an answar and it should be filled
in buffer.
the problem is that it is empty.

Full code :
#include <iostream>
#include <curl/curl.h>

using namespace std;
static char errorBuffer[CURL_ERROR_SIZE];
static std::string buffer;
static size_t write_to_buffer ( void *ptr, size_t size, size_t nmemb,
void *stream )
{
    if ( stream == NULL )
        return 0;

    ( ( std::string * ) stream )->append ( ( const char * ) ptr,size *
nmemb );

    return size * nmemb;

}

int main ( int argc, char *argv[] )
{

    CURL *m_curl; //Default handler
    CURLcode m_res; //Stores Result of Request
    string foo;
    FILE * fp;
    curl_global_init ( CURL_GLOBAL_ALL );

    /* get a curl handle */
    m_curl = curl_easy_init();

    if ( m_curl == NULL )
    {
        // Some Error handaling.
        fprintf ( stderr, "eror starting easy init" );
        return -1;
    }
    //Set the error buffer
    m_res = curl_easy_setopt ( m_curl, CURLOPT_ERRORBUFFER, errorBuffer );

    if ( m_res != CURLE_OK )
    {
        fprintf ( stderr,"Faile to set error buffer" );
        return -1;
    }
    curl_easy_setopt ( m_curl, CURLOPT_POSTFIELDS,
"parent_pid=&format=text&code2=This+message+was+sent+by+libcurl&poster=foo&paste=Send&expiry=m&email="
);
    curl_easy_setopt ( m_curl, CURLOPT_URL,
"http://pastebin.com/pastebin.php" );
    if ( m_res != CURLE_OK )
    {
        fprintf ( stderr,"Failed to set CURLOPT_URL the error was : %s"
, errorBuffer );
        return -1;
    }

    m_res = curl_easy_setopt ( m_curl, CURLOPT_NOPROGRESS, 1L );
    if ( m_res != CURLE_OK )
    {
        fprintf ( stderr,"Failed to set CURLOPT_NOPROGRESS, the error
was : %s" , errorBuffer );

    }
    m_res = curl_easy_setopt ( m_curl,
CURLOPT_WRITEFUNCTION,write_to_buffer );
    if ( m_res != CURLE_OK )
    {
        fprintf ( stderr,"Failed to set CURLOPT_WRITEFUNCTION, the
error was : %s" , errorBuffer );
        return -1;
    }

    m_res= curl_easy_setopt ( m_curl, CURLOPT_WRITEDATA, &buffer );
    if ( m_res != CURLE_OK )
    {
        fprintf ( stderr,"Failed to set CURLOPT_WRITEHEADER, the error
was : %s" , errorBuffer );
        return -1;
    }

    curl_easy_perform ( m_curl ); /* post away! */
    cerr<<buffer;

    return 0;
}
Received on 2008-07-27