Пример доступа к данным из C/C++ — различия между версиями
Материал из WebHMI Wiki
(Отметить эту версию для перевода) |
|||
Строка 1: | Строка 1: | ||
<translate> | <translate> | ||
+ | <!--T:1--> | ||
Пример кода на языке C для выполнения запроса к API WebHMI. | Пример кода на языке C для выполнения запроса к API WebHMI. | ||
+ | <!--T:2--> | ||
<pre> | <pre> | ||
#include <cstdlib> | #include <cstdlib> | ||
Строка 9: | Строка 11: | ||
#include <fstream> | #include <fstream> | ||
+ | <!--T:3--> | ||
using namespace std; | using namespace std; | ||
size_t write_func(void *ptr, size_t size, size_t nmemb, void *userdata) | size_t write_func(void *ptr, size_t size, size_t nmemb, void *userdata) | ||
Строка 16: | Строка 19: | ||
} | } | ||
+ | <!--T:4--> | ||
int main(int argc, char** argv) { | int main(int argc, char** argv) { | ||
Строка 21: | Строка 25: | ||
CURLcode res; | CURLcode res; | ||
− | char errorBuffer[CURL_ERROR_SIZE]; | + | <!--T:5--> |
+ | char errorBuffer[CURL_ERROR_SIZE]; | ||
− | // Create the GET request | + | <!--T:6--> |
+ | // Create the GET request | ||
struct curl_slist *headers = NULL; | struct curl_slist *headers = NULL; | ||
headers = curl_slist_append(headers, "X-WH-APIKEY: F3C74230818DA487BB2017CE5D0290F4DABCAFD7"); // API Key. If API Key is wrong API will return 401 status code. | headers = curl_slist_append(headers, "X-WH-APIKEY: F3C74230818DA487BB2017CE5D0290F4DABCAFD7"); // API Key. If API Key is wrong API will return 401 status code. | ||
Строка 32: | Строка 38: | ||
− | // Init CURL | + | <!--T:7--> |
+ | // Init CURL | ||
curl = curl_easy_init(); | curl = curl_easy_init(); | ||
− | if(curl) { | + | <!--T:8--> |
+ | if(curl) { | ||
curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.1/api/event-data/1"); // 1 = ID of report | curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.1/api/event-data/1"); // 1 = ID of report | ||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); | curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); | ||
Строка 41: | Строка 49: | ||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_func); | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_func); | ||
− | // Attempt to Connect the Server | + | <!--T:9--> |
+ | // Attempt to Connect the Server | ||
res = curl_easy_perform(curl); | res = curl_easy_perform(curl); | ||
− | if (res == CURLE_OK) { | + | <!--T:10--> |
+ | if (res == CURLE_OK) { | ||
long http_code = 0; | long http_code = 0; | ||
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code); | curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code); | ||
Строка 56: | Строка 66: | ||
} | } | ||
− | // Close the connection | + | <!--T:11--> |
+ | // Close the connection | ||
curl_easy_cleanup(curl); | curl_easy_cleanup(curl); | ||
} | } |
Версия 11:53, 1 марта 2018
Пример кода на языке C для выполнения запроса к API WebHMI.
#include <cstdlib> #include <iostream> #include <curl/curl.h> #include <curl/easy.h> #include <fstream> using namespace std; size_t write_func(void *ptr, size_t size, size_t nmemb, void *userdata) { cout.write( (char * ) ptr, size*nmemb); // just output response to screen return size * nmemb; } int main(int argc, char** argv) { CURL *curl; CURLcode res; char errorBuffer[CURL_ERROR_SIZE]; // Create the GET request struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "X-WH-APIKEY: F3C74230818DA487BB2017CE5D0290F4DABCAFD7"); // API Key. If API Key is wrong API will return 401 status code. headers = curl_slist_append(headers, "X-WH-START: 1391165350"); // Start time for report. Unixtime headers = curl_slist_append(headers, "X-WH-END: 1391186951"); // End time for report. Unixtime headers = curl_slist_append(headers, "Accept: application/json"); // Currenlty we support JSON ONLY, required header headers = curl_slist_append(headers, "Content-Type: application/json"); // Currenlty we support JSON ONLY, required header // Init CURL curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.1/api/event-data/1"); // 1 = ID of report curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_func); // Attempt to Connect the Server res = curl_easy_perform(curl); if (res == CURLE_OK) { long http_code = 0; curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code); if (http_code == 200 && res != CURLE_ABORTED_BY_CALLBACK) { cout << endl << "Success!"<< endl; } else { cout << endl << "Error! Code: " << http_code << endl; } } else { cout << "Connection Failed! Error: " << errorBuffer << endl; } // Close the connection curl_easy_cleanup(curl); } return 0; }