C Requires Chilkat v11.0.0+
C
Send an MMS with Twilio using a media URL
See more Twilio Examples
Send an image and an optional caption from an MMS-capable Twilio number. Supply an MMS-supported destination in E.164 format and replace the example media URL with an HTTPS URL Twilio can fetch. The host must handle HEAD and GET and return the correct Content-Type for the file.
Live send. With live credentials and valid inputs, this request submits a billable message. Save the Message SID to check delivery later. Supported media types and size limits.
Chilkat C Downloads
#include <C_CkHttp.h>
#include <C_CkStringBuilder.h>
#include <C_CkHttpRequest.h>
#include <C_CkHttpResponse.h>
#include <C_CkJsonObject.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttp http;
const char *accountSid;
HCkStringBuilder url;
HCkHttpRequest req;
HCkHttpResponse resp;
HCkJsonObject reply;
BOOL parsed;
success = FALSE;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
http = CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,TRUE);
CkHttp_putBasicAuth(http,TRUE);
// Load the owning Account SID and an API key from application configuration.
accountSid = "YOUR_ACCOUNT_SID";
CkHttp_putLogin(http,"YOUR_API_KEY_SID");
CkHttp_putPassword(http,"YOUR_API_KEY_SECRET");
url = CkStringBuilder_Create();
CkStringBuilder_Append(url,"https://api.twilio.com/2010-04-01/Accounts/");
CkStringBuilder_Append(url,accountSid);
CkStringBuilder_Append(url,"/Messages.json");
// Add raw field values. Chilkat form-encodes them, including the + in a phone number.
req = CkHttpRequest_Create();
CkHttpRequest_putHttpVerb(req,"POST");
CkHttpRequest_putContentType(req,"application/x-www-form-urlencoded");
CkHttpRequest_AddParam(req,"From","YOUR_MMS_ENABLED_NUMBER");
CkHttpRequest_AddParam(req,"To","RECIPIENT_E164_NUMBER");
// Supply a URL for the file itself so Twilio can retrieve the media.
CkHttpRequest_AddParam(req,"MediaUrl","https://media.example.com/photo.jpg");
CkHttpRequest_AddParam(req,"Body","Photo attached.");
resp = CkHttpResponse_Create();
reply = CkJsonObject_Create();
success = CkHttp_HttpReq(http,CkStringBuilder_getAsString(url),req,resp);
if (CkHttpResponse_getStatusCode(resp) == 0) {
printf("%s\n",CkHttp_lastErrorText(http));
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpRequest_Dispose(req);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(reply);
return;
}
parsed = CkJsonObject_Load(reply,CkHttpResponse_bodyStr(resp));
if ((success != TRUE) || (CkHttpResponse_getStatusCode(resp) != 201)) {
printf("HTTP %d\n",CkHttpResponse_getStatusCode(resp));
if ((parsed == TRUE) && (CkJsonObject_HasMember(reply,"code") == TRUE)) {
printf("Twilio error %d: %s\n",CkJsonObject_IntOf(reply,"code"),CkJsonObject_stringOf(reply,"message"));
}
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpRequest_Dispose(req);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(reply);
return;
}
if (parsed != TRUE) {
printf("Expected a JSON response.\n");
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpRequest_Dispose(req);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(reply);
return;
}
if ((CkJsonObject_JsonTypeOf(reply,"sid") != 1) || (CkJsonObject_JsonTypeOf(reply,"status") != 1)) {
printf("The create response is missing the message SID or status.\n");
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpRequest_Dispose(req);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(reply);
return;
}
printf("Message SID: %s\n",CkJsonObject_stringOf(reply,"sid"));
printf("Initial status: %s\n",CkJsonObject_stringOf(reply,"status"));
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpRequest_Dispose(req);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(reply);
}