Advertisement
Guest User

LIBCURL + C + POST

a guest
Mar 11th, 2016
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <curl/curl.h>
  3.  
  4. int main( void )
  5. {
  6.    int a = 1, b, c;
  7.    char post[1000];
  8.    CURL *curl;
  9.    CURLcode res;
  10.  
  11.    /* In windows, this will init the winsock stuff */
  12.    curl_global_init( CURL_GLOBAL_ALL );
  13.  
  14.    /* get a curl handle */
  15.    curl = curl_easy_init();
  16.    if ( curl ) {
  17.       // aqui é definido o formulario que recebera os dados da requisição (POST).
  18.       curl_easy_setopt( curl, CURLOPT_URL, "http://127.0.0.1/triangulo.php" );
  19.  
  20.       while ( a > 0 ) {
  21.          printf( "\nDigite os 3 lados do triangulo separados por espaco: " );
  22.          scanf( "%d %d %d", &a, &b, &c );
  23.  
  24.          sprintf( post, "n1=%d&n2=%d&n3=%d", a, b, c ); //Aqui é definida as variaveis do POST
  25.  
  26.          curl_easy_setopt( curl, CURLOPT_POSTFIELDS, post );
  27.  
  28.          /* Perform the request, res will get the return code */
  29.          res = curl_easy_perform( curl );
  30.  
  31.          if ( res != CURLE_OK ) // verifica se deu algum erro
  32.             fprintf( stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror( res ) );
  33.  
  34.       }
  35.  
  36.       /* always cleanup */
  37.       curl_easy_cleanup( curl );
  38.    }
  39.    curl_global_cleanup();
  40.    return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement