Sample code for 30+ languages & platforms
Xbase++

Clickatell Send SMS Text Message

See more Clickatell Examples

Demonstrate how to send a Clickatell SMS text message.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL oJson
LOCAL oResp
LOCAL oSbResponseBody
LOCAL oJResp
LOCAL nRespStatusCode
LOCAL cErrorCode
LOCAL cError
LOCAL cErrorDescription
LOCAL i
LOCAL nCount_i
LOCAL cApiMessageId
LOCAL nAccepted
LOCAL cV_to

nSuccess := 0

//  This example assumes the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

oHttp := CreateObject("Chilkat.Http")

//  Implements the following CURL command:

//  curl -i \
//  -X POST \
//  -H "Content-Type: application/json" \
//  -H "Accept: application/json" \
//  -H "Authorization: API_KEY" \
//  -d '{"content": "Test Message Text", "to": ["PHONE_NUMBER"], "from": "FROM_PHONE_NUMBER" }' \
//  -s https://platform.clickatell.com/messages

//  Use this online tool to generate code from sample JSON:
//  Generate Code to Create JSON

//  The following JSON is sent in the request body.

//  {
//    "content": "Test Message Text",
//    "to": [
//      "PHONE_NUMBER"
//    ]
//  }

oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("content", "Test Message Text")
oJson:UpdateString("to[0]", "PHONE_NUMBER")
oJson:UpdateString("from", "FROM_PHONE_NUMBER")

oHttp:SetRequestHeader("Authorization", "API_KEY")
oHttp:SetRequestHeader("Accept", "application/json")
oHttp:SetRequestHeader("Content-Type", "application/json")

//  If the following URL does not work, then try "https://platform.clickatell.com/v1/message"
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpJson("POST", "https://platform.clickatell.com/messages", oJson, "application/json", oResp)
IF (nSuccess == 0)
    ? oHttp:LastErrorText
    oHttp:destroy()
    oJson:destroy()
    oResp:destroy()
    RETURN
ENDIF

oSbResponseBody := CreateObject("Chilkat.StringBuilder")
oResp:GetBodySb(oSbResponseBody)
oJResp := CreateObject("Chilkat.JsonObject")
oJResp:LoadSb(oSbResponseBody)
oJResp:EmitCompact := 0

? "Response Body:"
? oJResp:Emit()

nRespStatusCode := oResp:StatusCode
? "Response Status Code = " + Str(nRespStatusCode)
IF (nRespStatusCode >= 400)
    ? "Response Header:"
    ? oResp:Header
    ? "Failed."
    oHttp:destroy()
    oJson:destroy()
    oResp:destroy()
    oSbResponseBody:destroy()
    oJResp:destroy()
    RETURN
ENDIF

//  Sample JSON response:
//  (Sample code for parsing the JSON response is shown below)

//  {
//    "messages": [
//      {
//        "apiMessageId": "66c06ac68e5c4afb953ea9328babfde6",
//        "accepted": true,
//        "to": "16302581871",
//        "errorCode": null,
//        "error": null,
//        "errorDescription": null
//      }
//    ],
//    "errorCode": null,
//    "error": null,
//    "errorDescription": null
//  }

//  Sample code for parsing the JSON response...
//  Use the following online tool to generate parsing code from sample JSON:
//  Generate Parsing Code from JSON

cErrorCode := oJResp:StringOf("errorCode")
cError := oJResp:StringOf("error")
cErrorDescription := oJResp:StringOf("errorDescription")
i := 0
nCount_i := oJResp:SizeOfArray("messages")
DO WHILE i < nCount_i
    oJResp:I := i
    cApiMessageId := oJResp:StringOf("messages[i].apiMessageId")
    nAccepted := oJResp:BoolOf("messages[i].accepted")
    cV_to := oJResp:StringOf("messages[i].to")
    cErrorCode := oJResp:StringOf("messages[i].errorCode")
    cError := oJResp:StringOf("messages[i].error")
    cErrorDescription := oJResp:StringOf("messages[i].errorDescription")
    i := i + 1
ENDDO

oHttp:destroy()
oJson:destroy()
oResp:destroy()
oSbResponseBody:destroy()
oJResp:destroy()