C 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://secure.smsfactor.com/token.html
  char* numbers[] = {"33600000000","33700000000"};
  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.smsfactor.com/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;

}