Skip to content

Instantly share code, notes, and snippets.

@whoshuu
Created March 31, 2015 06:44
Show Gist options
  • Save whoshuu/2dc858b8730079602044 to your computer and use it in GitHub Desktop.
Save whoshuu/2dc858b8730079602044 to your computer and use it in GitHub Desktop.
Example libcurl GET request
#include <curl/curl.h>
#include <string>
size_t writeFunction(void *ptr, size_t size, size_t nmemb, std::string* data) {
data->append((char*) ptr, size * nmemb);
return size * nmemb;
}
int main(int argc, char** argv) {
auto curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://api.github.com/repos/whoshuu/cpr/contributors?anon=true&key=value");
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(curl, CURLOPT_USERPWD, "user:pass");
curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/7.42.0");
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
std::string response_string;
std::string header_string;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFunction);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &header_string);
char* url;
long response_code;
double elapsed;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &elapsed);
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl = NULL;
}
}
@Mho782249119
Copy link

#include <curl/curl.h>//هاذا هو الصحيح
#include
#include

// دالة لتخزين استجابة السيرفر في متغير
size_t writeFunction(void* ptr, size_t size, size_t nmemb, std::string* data) {
data->append((char*)ptr, size * nmemb);
return size * nmemb;
}

int main() {
// تهيئة curl
CURL* curl = curl_easy_init();
if (curl) {
// URL الذي تريد الوصول إليه
const char* url = "https://api.github.com/repos/whoshuu/cpr/contributors?anon=true&key=value";

    // إعداد الخيارات
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/7.42.0");
    curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);

    // لتخزين الاستجابة
    std::string response_string;
    std::string header_string;

    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFunction);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string);

    // تنفيذ الطلب
    CURLcode res = curl_easy_perform(curl);

    // معالجة النتائج
    if (res != CURLE_OK) {
        std::cerr << "Curl failed: " << curl_easy_strerror(res) << std::endl;
    } else {
        long response_code;
        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);

        std::cout << "Response Code: " << response_code << std::endl;
        std::cout << "Response Body: " << response_string << std::endl;
    }

    // تنظيف الموارد
    curl_easy_cleanup(curl);
} else {
    std::cerr << "Failed to initialize CURL" << std::endl;
}

return 0;

}

@Mho782249119
Copy link

#include <curl/curl.h>
#include
#include

// دالة لتخزين استجابة السيرفر في متغير
size_t writeFunction(void* ptr, size_t size, size_t nmemb, std::string* data) {
data->append((char*)ptr, size * nmemb);
return size * nmemb;
}

int main() {
// تهيئة curl
CURL* curl = curl_easy_init();
if (curl) {
// URL الذي تريد الوصول إليه
const char* url = "https://api.github.com/repos/whoshuu/cpr/contributors?anon=true&key=value";

    // إعداد الخيارات
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/7.42.0");
    curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);

    // لتخزين الاستجابة
    std::string response_string;
    std::string header_string;

    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFunction);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string);

    // تنفيذ الطلب
    CURLcode res = curl_easy_perform(curl);

    // معالجة النتائج
    if (res != CURLE_OK) {
        std::cerr << "Curl failed: " << curl_easy_strerror(res) << std::endl;
    } else {
        long response_code;
        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);

        std::cout << "Response Code: " << response_code << std::endl;
        std::cout << "Response Body: " << response_string << std::endl;
    }

    // تنظيف الموارد
    curl_easy_cleanup(curl);
} else {
    std::cerr << "Failed to initialize CURL" << std::endl;
}

return 0;

}

@1Fiolet1
Copy link

Hello from ISy28, 2 sem!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment