SMS C API example


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include "cJSON.h"

int main()

{
  char token[] = "token"; //Your first token must be generated on our plateform at https://app.smsup.ch/developers/api-tokens
  char* numbers[] = {"41781234567","41781234567"};
  char data[] = "C example test";

  char src[200];
  int i = 0;
  char *out;
  cJSON *postdata, *sms, *recipients, *car, *message, *gsm;
  struct curl_slist *list = NULL;
  CURLcode response;

  postdata = cJSON_CreateObject();
  sms = cJSON_CreateObject();
  recipients =cJSON_CreateObject();
  message = cJSON_CreateObject();
  gsm = cJSON_CreateArray();

  cJSON_AddItemToObject(postdata, "sms", sms);
  cJSON_AddItemToObject(sms, "recipients", recipients);
  cJSON_AddItemToObject(sms, "message", message);
  cJSON_AddItemToObject(recipients, "gsm", gsm);

  for(i = 0 ; i < sizeof(numbers)/sizeof(char*) ; i++){
    cJSON_AddItemToArray(gsm, car = cJSON_CreateObject());
    cJSON_AddItemToObject(car, "value", cJSON_CreateString(numbers[i]));
  }
  cJSON_AddItemToObject(message, "text", cJSON_CreateString(data));

  out = cJSON_Print(postdata);
  printf("%s\n", out);
  strcpy(src,  "Authorization: Bearer ");
  strcat(src, token);
  
  list = curl_slist_append(list, "Content-Type: application/json");
  list = curl_slist_append(list, "Accept: application/json");
  list = curl_slist_append(list,src);

  CURL *curl = curl_easy_init();

  if(curl){
    curl_easy_setopt(curl, CURLOPT_URL, "https://api.smsup.ch/send");
    curl_easy_setopt(curl, CURLOPT_POST, 1);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE,(long) strlen(out));
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, out);
    response = curl_easy_perform(curl);

        if (response != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(response));
        }
        curl_easy_cleanup(curl);
    }

  cJSON_Delete(postdata);
  curl_slist_free_all(list);

  return 0;

}