Advertisement
glauco486

Untitled

Mar 18th, 2011
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <curl/curl.h>
  4.  
  5. #define END_POINT "http://twitpic.com/api/upload"
  6. #define EXPECT_ARGS 4
  7.  
  8. size_t write_data(void *buffer, size_t size, size_t nmemb, void* userp)
  9. {
  10.  
  11.     std::stringstream strmResponse;
  12.     size_t nReal = size * nmemb;
  13.     strmResponse << std::string((char*)buffer, size*nmemb);
  14.     std::string sLine("");
  15.     while (getline(strmResponse, sLine)) {
  16.         std::cout << sLine.c_str() << std::endl;
  17.     }
  18.     return nReal;
  19. }
  20.  
  21. int main (int argc, char * const argv[]) {
  22.  
  23.     int result = 0;
  24.     CURL* hCurl = NULL;
  25.     CURLcode hResult;
  26.     std::string sUser("");
  27.     std::string sPass("");
  28.     std::string sFileName("");
  29.     //Curl for form data
  30.     struct curl_httppost *post = NULL;
  31.     struct curl_httppost *last = NULL;
  32.     try{
  33.         if(argc < EXPECT_ARGS) {
  34.             std::cout << "Usage twitpic_upload twitterUserName twiiterPassword fileName" << std::endl;
  35.             throw false;
  36.         }
  37.         //Get the require params from command line args
  38.         sUser     = argv[1];
  39.         sPass     = argv[2];
  40.         sFileName = argv[3];
  41.  
  42.         //Initialize curl, just don't let easy_init to do it for you
  43.         curl_global_init(CURL_GLOBAL_ALL);
  44.         //Handle to the curl
  45.         hCurl = curl_easy_init();
  46.         if(NULL == hCurl) {
  47.             throw false;
  48.         }
  49.         //Construct the form
  50.         curl_formadd(&post, &last, CURLFORM_COPYNAME, "username", CURLFORM_COPYCONTENTS, sUser.c_str(), CURLFORM_END);
  51.         curl_formadd(&post, &last, CURLFORM_COPYNAME, "password", CURLFORM_COPYCONTENTS, sPass.c_str(), CURLFORM_END);
  52.         curl_formadd(&post, &last, CURLFORM_COPYNAME, "media", CURLFORM_FILE, sFileName.c_str(), CURLFORM_END);
  53.         //Specify the API Endpoint
  54.         hResult = curl_easy_setopt(hCurl,CURLOPT_URL, END_POINT);
  55.         //Specify the HTTP Method
  56.         hResult = curl_easy_setopt(hCurl, CURLOPT_HTTPPOST, post);
  57.         //Post Away !!!
  58.         hResult = curl_easy_perform(hCurl);
  59.         if(hResult != CURLE_OK){
  60.             std::cout << "Cannot find the twitpic site " << std::endl;
  61.             throw false;
  62.         }
  63.     }
  64.     catch (...) {
  65.         result = -1;
  66.     }
  67.     curl_formfree(post);
  68.     curl_easy_cleanup(hCurl);
  69.     curl_global_cleanup();
  70.    
  71.     system("PAUSE");
  72.     return result;
  73. }
  74.  
  75.  
  76. // Glauco486 twitpic
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement