Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) Twilio Send SMS (using Chilkat HTTP)Send an outgoing SMS message. For more information, see https://www.twilio.com/docs/sms/api/message-resource#create-a-message-resource
#include <C_CkHttpW.h> #include <C_CkHttpRequestW.h> #include <C_CkHttpResponseW.h> #include <C_CkStringBuilderW.h> #include <C_CkJsonObjectW.h> void ChilkatSample(void) { HCkHttpW http; BOOL success; HCkHttpRequestW req; HCkHttpResponseW resp; HCkStringBuilderW sbResponseBody; HCkJsonObjectW jResp; int respStatusCode; const wchar_t *account_sid; const wchar_t *api_version; const wchar_t *body; const wchar_t *date_created; const wchar_t *date_sent; const wchar_t *date_updated; const wchar_t *direction; const wchar_t *error_code; const wchar_t *error_message; const wchar_t *from; const wchar_t *messaging_service_sid; const wchar_t *num_media; const wchar_t *num_segments; const wchar_t *price; const wchar_t *price_unit; const wchar_t *sid; const wchar_t *status; const wchar_t *subresource_urisMedia; const wchar_t *v_to; const wchar_t *uri; // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. http = CkHttpW_Create(); // Implements the following CURL command: // (See information about using test credentials and phone numbers: https://www.twilio.com/docs/iam/test-credentials) // curl -X POST https://api.twilio.com/2010-04-01/Accounts/TWILIO_ACCOUNT_SID/Messages.json \ // --data-urlencode "From=+15005550006" \ // --data-urlencode "Body=body" \ // --data-urlencode "To=+15005551212" \ // -u TWILIO_ACCOUNT_SID:TWILIO_AUTH_TOKEN // Use the following online tool to generate HTTP code from a CURL command // Convert a cURL Command to HTTP Source Code CkHttpW_putLogin(http,L"TWILIO_ACCOUNT_SID"); CkHttpW_putPassword(http,L"TWILIO_AUTH_TOKEN"); req = CkHttpRequestW_Create(); CkHttpRequestW_putHttpVerb(req,L"POST"); CkHttpRequestW_putPath(req,L"/2010-04-01/Accounts/TWILIO_ACCOUNT_SID/Messages.json"); CkHttpRequestW_putContentType(req,L"application/x-www-form-urlencoded"); CkHttpRequestW_AddParam(req,L"From",L"+15005550006"); CkHttpRequestW_AddParam(req,L"Body",L"body"); CkHttpRequestW_AddParam(req,L"To",L"+15005551212"); resp = CkHttpW_PostUrlEncoded(http,L"https://api.twilio.com/2010-04-01/Accounts/TWILIO_ACCOUNT_SID/Messages.json",req); if (CkHttpW_getLastMethodSuccess(http) == FALSE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); return; } sbResponseBody = CkStringBuilderW_Create(); CkHttpResponseW_GetBodySb(resp,sbResponseBody); jResp = CkJsonObjectW_Create(); CkJsonObjectW_LoadSb(jResp,sbResponseBody); CkJsonObjectW_putEmitCompact(jResp,FALSE); wprintf(L"Response Body:\n"); wprintf(L"%s\n",CkJsonObjectW_emit(jResp)); // A 201 status code indicates success. respStatusCode = CkHttpResponseW_getStatusCode(resp); wprintf(L"Response Status Code = %d\n",respStatusCode); if (respStatusCode >= 400) { wprintf(L"Response Header:\n"); wprintf(L"%s\n",CkHttpResponseW_header(resp)); wprintf(L"Failed.\n"); CkHttpResponseW_Dispose(resp); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); return; } CkHttpResponseW_Dispose(resp); // Sample JSON response: // (Sample code for parsing the JSON response is shown below) // { // "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", // "api_version": "2010-04-01", // "body": "body", // "date_created": "Thu, 30 Jul 2015 20:12:31 +0000", // "date_sent": "Thu, 30 Jul 2015 20:12:33 +0000", // "date_updated": "Thu, 30 Jul 2015 20:12:33 +0000", // "direction": "outbound-api", // "error_code": null, // "error_message": null, // "from": "+15017122661", // "messaging_service_sid": "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", // "num_media": "0", // "num_segments": "1", // "price": null, // "price_unit": null, // "sid": "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", // "status": "sent", // "subresource_uris": { // "media": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Media.json" // }, // "to": "+15558675310", // "uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json" // } // Sample code for parsing the JSON response... // Use the following online tool to generate parsing code from sample JSON: // Generate Parsing Code from JSON // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat. // See this example explaining how this memory should be used: const char * functions. account_sid = CkJsonObjectW_stringOf(jResp,L"account_sid"); api_version = CkJsonObjectW_stringOf(jResp,L"api_version"); body = CkJsonObjectW_stringOf(jResp,L"body"); date_created = CkJsonObjectW_stringOf(jResp,L"date_created"); date_sent = CkJsonObjectW_stringOf(jResp,L"date_sent"); date_updated = CkJsonObjectW_stringOf(jResp,L"date_updated"); direction = CkJsonObjectW_stringOf(jResp,L"direction"); error_code = CkJsonObjectW_stringOf(jResp,L"error_code"); error_message = CkJsonObjectW_stringOf(jResp,L"error_message"); from = CkJsonObjectW_stringOf(jResp,L"from"); messaging_service_sid = CkJsonObjectW_stringOf(jResp,L"messaging_service_sid"); num_media = CkJsonObjectW_stringOf(jResp,L"num_media"); num_segments = CkJsonObjectW_stringOf(jResp,L"num_segments"); price = CkJsonObjectW_stringOf(jResp,L"price"); price_unit = CkJsonObjectW_stringOf(jResp,L"price_unit"); sid = CkJsonObjectW_stringOf(jResp,L"sid"); status = CkJsonObjectW_stringOf(jResp,L"status"); subresource_urisMedia = CkJsonObjectW_stringOf(jResp,L"subresource_uris.media"); v_to = CkJsonObjectW_stringOf(jResp,L"to"); uri = CkJsonObjectW_stringOf(jResp,L"uri"); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.