Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=====================================================================================================================================
- std::string postthis = "";
- postthis += "group_id=111074163&access_token=[ТОКЕН]&v=5.59";
- // получаю и вывожу адрес для загрузки фото:
- std::string upload_url = Programm::getWallUploadServer(postthis);
- richTextBox1->Text = gcnew System::String(upload_url.c_str());// вывод результата
- // получаю полный путь фотографии
- TCHAR szFileName[MAX_PATH], szPath[MAX_PATH];
- GetModuleFileName(0, szFileName, MAX_PATH);
- Programm::ExtractFilePath(szFileName, szPath);
- strcat(szPath, "1.jpg");
- // отправляю фото [(адрес загрузки фото на сервер вк, полный путь фото на диске)]
- std::string res = Programm::UploadPhoto(upload_url, std::string(szPath));
- richTextBox1->Text += "\n\n" + gcnew System::String(res.c_str());// вывод результата
- //=====================================================================================================================================
- // класс Programm:
- std::string Programm::getWallUploadServer(std::string fields)
- {
- std::string content;
- char temp[50];
- CURL *curl = curl_easy_init();
- if (curl)
- {
- //----------------------------------------------------------------------------------------
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
- curl_easy_setopt(curl, CURLOPT_URL, "https://api.vk.com/method/photos.getWallUploadServer.xml?");
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fields.c_str());
- curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)fields.size());
- // сохраняем html код cтраницы в строку content
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
- // Загловок ответа сервера выводим в консоль
- // curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_head);
- // curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &std::cout);
- CURLcode res = curl_easy_perform(curl);
- if (res != CURLE_OK)
- {
- sprintf(temp, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); // fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
- return std::string(temp);
- }
- // return content;
- curl_easy_cleanup(curl);
- }
- // парсинг ссылки:
- XML::XMLDocument xml;
- xml.Parse(content.c_str());
- if (xml.FirstChildElement("response")->FirstChildElement("upload_url") != nullptr)
- {
- return content = std::string(xml.FirstChildElement("response")->FirstChildElement("upload_url")->GetText());
- }
- else return content = "error parse";
- }
- std::string Programm::UploadPhoto(std::string upload_url, std::string file)
- {
- std::string content, fields = "photo" + file;
- String^ managed = msclr::interop::marshal_as<String^>(file.c_str());
- if (!File::Exists(managed)) { return content = "no_photo"; }
- //----------------------------------------------------------
- CURL* curl;
- char buff[200];
- struct curl_httppost* post = NULL;
- struct curl_httppost* last = NULL;
- curl_global_init(CURL_GLOBAL_ALL);
- curl = curl_easy_init();
- curl_formadd(&post, &last, CURLFORM_COPYNAME, "photo", CURLFORM_FILE, file.c_str(), CURLFORM_CONTENTTYPE, "multipart/form-data", CURLFORM_END);
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
- curl_easy_setopt(curl, CURLOPT_URL, upload_url.c_str());
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fields.c_str());
- curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)fields.size());
- // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
- // curl_easy_setopt(curl, CURLOPT_HEADER, 1);
- // 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");
- // curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
- // сохраняем html код cтраницы в строку content
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
- CURLcode res = curl_easy_perform(curl);
- if (res != CURLE_OK)
- {
- sprintf(buff, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); // fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
- return std::string(buff);
- }
- // else { return content = "res == CURLE_OK"; }
- return content;
- curl_easy_cleanup(curl);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement