Sending SMS in C++ using the libcurl API library

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Sending SMS in C++ using the libcurl library



In modern development, integration with external services for sending SMS messages is often required. In C++, this task can be efficiently solved using the libcurl library — a powerful tool for working with HTTP/HTTPS protocols. The libcurl library provides a simple and flexible API for making requests to REST services, including sending SMS via provider APIs.



What is libcurl?



libcurl is a cross-platform library for data transfer over various protocols (HTTP, HTTPS, FTP, SMTP, and others). It is widely used in C++ projects due to its stability, performance, and extensive documentation. For sending SMS, we will need the functionality of HTTP POST requests with JSON or URL-encoded data transmission.



Installing libcurl



In most Linux distributions, libcurl can be installed via the package manager:



sudo apt-get install libcurl4-openssl-dev  # Ubuntu/Debiansudo yum install libcurl-devel             # CentOS/RHEL


For Windows, you can use vcpkg or download pre-built binaries from the official curl.se website.



Basic example: sending SMS via HTTP POST



Let's consider sending SMS using the API of a popular service (e.g., Twilio, Nexmo, or a Russian equivalent). In the example below, we send a POST request with JSON data containing the recipient's phone number and the message text.



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

// Callback function for writing server responsestatic size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) { ((std::string*)userp)->append((char*)contents, size * nmemb); return size * nmemb;}

bool sendSms(const std::string& apiKey, const std::string& phone, const std::string& message) { CURL* curl; CURLcode res; std::string response; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if(curl) { // API endpoint URL (replace with yours) curl_easy_setopt(curl, CURLOPT_URL, "https://api.smsprovider.com/v1/send"); // Headers: content type and authorization struct curl_slist* headers = NULL; headers = curl_slist_append(headers, "Content-Type: application/json"); std::string authHeader = "Authorization: Bearer " + apiKey; headers = curl_slist_append(headers, authHeader.c_str()); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); // Form the JSON request body std::string jsonData = "{"; jsonData += "\\"phone\\":\\"" + phone + "\\","; jsonData += "\\"message\\":\\"" + message + "\\""; jsonData += "}"; curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonData.c_str()); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); // Perform the request res = curl_easy_perform(curl); // Check the result if(res != CURLE_OK) { std::cerr << "curl error: " << curl_easy_strerror(res) << std::endl; curl_easy_cleanup(curl); curl_global_cleanup(); return false; } // Get the HTTP response code long httpCode; curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode); curl_easy_cleanup(curl); curl_global_cleanup(); if(httpCode == 200 || httpCode == 201) { std::cout << "SMS sent successfully. Response: " << response << std::endl; return true; } else { std::cerr << "Server error. HTTP " << httpCode << ": " << response << std::endl;

Recommendations