-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest.c
43 lines (38 loc) · 1.16 KB
/
rest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Copyright 2015, William Dignazio
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#include <twilio.h>
/* Buils the entire resource uri for the twilio api function */
char*
build_uri(char *resource) {
char *base = malloc(strlen(BASEURL)+
strlen(asid) +
strlen(atoken) +
strlen(resource));
sprintf(base, BASEURL, asid, atoken, resource);
char *uri = malloc(strlen(base)+strlen(asid));
sprintf(uri, base, asid);
/* Make sure you free the memory later after using this. */
return uri;
}
/* POST message to send a sms message to the desired recpient */
void
post_sms(char *snd, char *rec, char *msg) {
char *uri = build_uri(POST_SMS);
char *bodybase = "From=%s&To=%s&Body=%s";
char *buf = malloc(strlen(bodybase)+
strlen(snd) +
strlen(rec) +
strlen(msg));
sprintf(buf, bodybase, snd, rec, msg);
/* Set the proper fields for posting. */
curl_easy_setopt(handle, CURLOPT_URL, uri);
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, buf);
curl_easy_perform(handle);
free(buf);
free(uri);
}