#include <iostream>
#include <curl/curl.h>
// ตัวอย่างการเรียกใช้ API ในหลายภาษา
int write_callback(char* data, size_t size, size_t nmemb, std::string* buffer) {
int result = 0;
if (buffer != nullptr) {
buffer->append(data, size * nmemb);
result = size * nmemb;
}
return result;
}
int main() {
// 1. Java (ตัวอย่างด้วย OkHttp)
curl_global_init(CURL_GLOBAL_DEFAULT);
// สร้างอินสแตนซ์ OkHttpClient และส่งข้อมูล JSON ผ่าน POST ไปยัง API ท้องถิ่น
std::string url = "http://example.com/post";
// 2. Python (ตัวอย่างด้วย Requests)
std::string json = "{\"name\": \"John\", \"age\": 30}";
// ใช้ไลบรารี Requests เพื่อส่งคำขอ POST พร้อมเนื้อหา JSON ไปยัง API ท้องถิ่น
CURL* curl = curl_easy_init();
if (curl) {
// 3. C# (ตัวอย่างด้วย .NET Core)
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
// ใช้คลาส HttpClient เพื่อทำการเรียกใช้ API แบบอะซิงโครนัส
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
// 4. E-Language (易语言)
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
std::cout << "Failed to send POST request: " << curl_easy_strerror(res) << std::endl;
}
// ใช้พอร์ต HTTP ภายในเพื่อส่งคำขอ POST และแยกวิเคราะห์ข้อมูล JSON ที่ส่งกลับมา
long http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
std::string response_data;
curl_easy_getinfo(curl, CURLINFO_PRIVATE, &response_data);
// 5. การควบคุมเบราว์เซอร์ด้วย Selenium (Python)
std::cout << "HTTP Status Code: " << http_code << std::endl;
std::cout << "Response Data: " << response_data << std::endl;
// หลังจากเปิดเบราว์เซอร์สำเร็จ ให้นำข้อมูล 'debugger_address' ที่ได้รับไปเชื่อมต่อกับ Remote WebDriver ของ Selenium เพื่อควบคุม
curl_easy_cleanup(curl);
}
// อธิบายโค้ดตัวอย่าง:
curl_global_cleanup();
return 0;
}