Sending SMS in C++: API Libraries libcurl and cpprestsdk
In modern C++ development, there is often a need to integrate with external services, including sending SMS messages. Although the C++ standard library does not provide built-in tools for working with HTTP requests, there are powerful third-party libraries that solve this problem. In this article, we will look at two popular libraries — libcurl (CURL) and cpprestsdk (Casablanca) — and show how to use them to send SMS via the APIs of popular SMS providers.
Why do you need a library for sending SMS in C++?
Sending SMS via C++ is a typical task for server applications, monitoring systems, notifications, or automation. Direct work with sockets and the HTTP protocol in C++ is complex and error-prone. Libraries such as libcurl and cpprestsdk abstract this complexity, providing a convenient interface for making HTTP requests (GET, POST, PUT) and processing JSON/XML responses.
We will use the API of an SMS provider (e.g., Twilio, SMS.ru, or any other that supports a REST API). In the examples below, replace YOUR_API_KEY, YOUR_SENDER, and YOUR_PHONE with your actual data.
Library 1: libcurl (CURL)
libcurl is a time-tested library for working with URLs, supporting many protocols (HTTP, HTTPS, FTP, etc.). It is written in C but has excellent C++ bindings. To use it in a C++ project, simply include the header file curl/curl.h and link against libcurl.
Installing libcurl
- Linux (Ubuntu/Debian):
sudo apt-get install libcurl4-openssl-dev - Windows: download pre-built binaries from the official website or use vcpkg:
vcpkg install curl - macOS:
brew install curl
Example of sending SMS via libcurl (C++ syntax)
In this example, we use a simple POST request with data in JSON format. The libcurl library handles HTTP headers and the request body.
#include <iostream>#include <string>#include <curl/curl.h>
// Callback function for writing the server responsesize_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* output) { size_t totalSize = size * nmemb; output->append((char*)contents, totalSize); return totalSize;}
bool sendSMS_via_libcurl(const std::string& phone, const std::string& message) { CURL* curl = curl_easy_init(); if (!curl) return false;
std::string response; std::string url = "https://api.sms-provider.com/v1/send"; // Replace with your provider's URL std::string jsonData = R"({ "api_key": "YOUR_API_KEY", "sender": "YOUR_SENDER", "phone": ")" + phone + R"(", "text": ")" + message + R"(" })";
struct curl_slist* headers = nullptr; headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonData.c_str()); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
CURLcode res = curl_easy_perform(curl); curl_slist_free_all(headers); curl_easy_cleanup(curl);
if (res != CURLE_OK) { std::cerr << "curl error: " << curl_easy_strerror(res) << std::endl; return false; }
std::cout << "Server response: " << response << std::endl; return true;}
int main() { curl_global_init(CURL_GLOBAL_DEF