Advertisement
Guest User

upload photo to server vk

a guest
Jul 19th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=====================================================================================================================================
  2.         std::string postthis = "";
  3.         postthis += "group_id=111074163&access_token=[ТОКЕН]&v=5.59";
  4. // получаю и вывожу адрес для загрузки фото:
  5.         std::string upload_url = Programm::getWallUploadServer(postthis);
  6.         richTextBox1->Text = gcnew System::String(upload_url.c_str());// вывод результата
  7. // получаю полный путь фотографии
  8.         TCHAR szFileName[MAX_PATH], szPath[MAX_PATH];
  9.         GetModuleFileName(0, szFileName, MAX_PATH);
  10.         Programm::ExtractFilePath(szFileName, szPath);
  11.         strcat(szPath, "1.jpg");
  12. // отправляю фото [(адрес загрузки фото на сервер вк, полный путь фото на диске)]
  13.         std::string res = Programm::UploadPhoto(upload_url, std::string(szPath));
  14.         richTextBox1->Text += "\n\n" + gcnew System::String(res.c_str());// вывод результата
  15.  
  16. //=====================================================================================================================================
  17. // класс Programm:
  18. std::string Programm::getWallUploadServer(std::string fields)
  19. {
  20.     std::string content;
  21.     char temp[50];
  22.     CURL *curl = curl_easy_init();
  23.     if (curl)
  24.     {
  25.  
  26.         //----------------------------------------------------------------------------------------
  27.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  28.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
  29.         curl_easy_setopt(curl, CURLOPT_URL, "https://api.vk.com/method/photos.getWallUploadServer.xml?");
  30.         curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fields.c_str());
  31.         curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)fields.size());
  32.  
  33.         //  сохраняем html код cтраницы в строку content
  34.         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  35.         curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
  36.         // Загловок ответа сервера выводим в консоль
  37.         //      curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_head);
  38.         //      curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &std::cout);
  39.        
  40.         CURLcode res = curl_easy_perform(curl);
  41.         if (res != CURLE_OK)
  42.         {
  43.             sprintf(temp, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); //  fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
  44.             return std::string(temp);
  45.         }
  46.     //  return content;
  47.         curl_easy_cleanup(curl);
  48.     }
  49.     // парсинг ссылки:
  50.     XML::XMLDocument xml;
  51.     xml.Parse(content.c_str());
  52.     if (xml.FirstChildElement("response")->FirstChildElement("upload_url") != nullptr)
  53.     {
  54.         return content = std::string(xml.FirstChildElement("response")->FirstChildElement("upload_url")->GetText());
  55.     }
  56.     else return content = "error parse";
  57. }
  58.  
  59. std::string Programm::UploadPhoto(std::string upload_url, std::string file)
  60. {
  61.     std::string content, fields = "photo" + file;
  62.     String^ managed = msclr::interop::marshal_as<String^>(file.c_str());
  63.     if (!File::Exists(managed)) { return content = "no_photo"; }
  64.  
  65. //----------------------------------------------------------
  66.     CURL* curl;
  67.     char buff[200];
  68.     struct curl_httppost* post = NULL;
  69.     struct curl_httppost* last = NULL;
  70.  
  71.     curl_global_init(CURL_GLOBAL_ALL);
  72.     curl = curl_easy_init();
  73.     curl_formadd(&post, &last, CURLFORM_COPYNAME, "photo", CURLFORM_FILE, file.c_str(), CURLFORM_CONTENTTYPE, "multipart/form-data", CURLFORM_END);
  74.     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  75.     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
  76.     curl_easy_setopt(curl, CURLOPT_URL, upload_url.c_str());
  77.     curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fields.c_str());
  78.     curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)fields.size());
  79. //  curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  80. //  curl_easy_setopt(curl, CURLOPT_HEADER, 1);
  81. //  curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
  82. //  curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
  83.    
  84. //  сохраняем html код cтраницы в строку content
  85.     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  86.     curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
  87.  
  88.     CURLcode res = curl_easy_perform(curl);
  89.  
  90.     if (res != CURLE_OK)
  91.     {
  92.         sprintf(buff, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); //  fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
  93.         return std::string(buff);
  94.     }
  95. //  else { return content = "res == CURLE_OK"; }
  96.     return content;
  97.     curl_easy_cleanup(curl);
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement