Sample code for 30+ languages & platforms
PureBasic

SMSAPI - List Contacts

See more SMSAPI Examples

List Contacts

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkDtObj.pb"

Procedure ChilkatExample()

    success.i = 0

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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Implements the following CURL command:

    ; curl -X GET -H "Authorization: Bearer token_api_oauth" https://api.smsapi.com/contacts?phone_number=48500000000

    ; Use the following online tool to generate HTTP code from a CURL command
    ; Convert a cURL Command to HTTP Source Code

    ; Adds the "Authorization: Bearer token_api_oauth" header.
    CkHttp::setCkAuthToken(http, "token_api_oauth")

    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckQuickGetSb(http,"https://api.smsapi.com/contacts?phone_number=48500000000",sbResponseBody)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

    jResp.i = CkJsonObject::ckCreate()
    If jResp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(jResp,sbResponseBody)
    CkJsonObject::setCkEmitCompact(jResp, 0)

    Debug "Response Body:"
    Debug CkJsonObject::ckEmit(jResp)

    respStatusCode.i = CkHttp::ckLastStatus(http)
    Debug "Response Status Code = " + Str(respStatusCode)
    If respStatusCode >= 400
        Debug "Response Header:"
        Debug CkHttp::ckLastHeader(http)
        Debug "Failed."
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbResponseBody)
        CkJsonObject::ckDispose(jResp)
        ProcedureReturn
    EndIf

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

    ; {
    ;   "size": 1,
    ;   "collection": [
    ;     {
    ;       "id": "5b83ba81a788494a0469490f",
    ;       "first_name": "name",
    ;       "last_name": "surname",
    ;       "phone_number": "48500000000",
    ;       "email": "bok@smsapi.com",
    ;       "gender": "male",
    ;       "city": "City",
    ;       "country": "Poland",
    ;       "source": "source",
    ;       "date_created": "2018-08-27T10:46:57+02:00",
    ;       "date_updated": "2018-08-27T10:46:57+02:00",
    ;       "groups": [
    ;         {
    ;           "id": "59a3ca1fa78849062837cd0c",
    ;           "name": "default",
    ;           "date_created": "2017-08-28T09:45:35+02:00",
    ;           "date_updated": "2017-08-28T09:45:35+02:00",
    ;           "description": "",
    ;           "created_by": "login",
    ;           "idx": null,
    ;           "contact_expire_after": null,
    ;           "contacts_count": 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

    date_created.i = CkDtObj::ckCreate()
    If date_created.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    date_updated.i = CkDtObj::ckCreate()
    If date_updated.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    id.s
    first_name.s
    last_name.s
    phone_number.s
    email.s
    gender.s
    city.s
    country.s
    source.s
    j.i
    count_j.i
    name.s
    description.s
    created_by.s
    idx.s
    contact_expire_after.s
    contacts_count.s

    size.i = CkJsonObject::ckIntOf(jResp,"size")
    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jResp,"collection")
    While i < count_i
        CkJsonObject::setCkI(jResp, i)
        id = CkJsonObject::ckStringOf(jResp,"collection[i].id")
        first_name = CkJsonObject::ckStringOf(jResp,"collection[i].first_name")
        last_name = CkJsonObject::ckStringOf(jResp,"collection[i].last_name")
        phone_number = CkJsonObject::ckStringOf(jResp,"collection[i].phone_number")
        email = CkJsonObject::ckStringOf(jResp,"collection[i].email")
        gender = CkJsonObject::ckStringOf(jResp,"collection[i].gender")
        city = CkJsonObject::ckStringOf(jResp,"collection[i].city")
        country = CkJsonObject::ckStringOf(jResp,"collection[i].country")
        source = CkJsonObject::ckStringOf(jResp,"collection[i].source")
        CkJsonObject::ckDtOf(jResp,"collection[i].date_created",0,date_created)
        CkJsonObject::ckDtOf(jResp,"collection[i].date_updated",0,date_updated)
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(jResp,"collection[i].groups")
        While j < count_j
            CkJsonObject::setCkJ(jResp, j)
            id = CkJsonObject::ckStringOf(jResp,"collection[i].groups[j].id")
            name = CkJsonObject::ckStringOf(jResp,"collection[i].groups[j].name")
            CkJsonObject::ckDtOf(jResp,"collection[i].groups[j].date_created",0,date_created)
            CkJsonObject::ckDtOf(jResp,"collection[i].groups[j].date_updated",0,date_updated)
            description = CkJsonObject::ckStringOf(jResp,"collection[i].groups[j].description")
            created_by = CkJsonObject::ckStringOf(jResp,"collection[i].groups[j].created_by")
            idx = CkJsonObject::ckStringOf(jResp,"collection[i].groups[j].idx")
            contact_expire_after = CkJsonObject::ckStringOf(jResp,"collection[i].groups[j].contact_expire_after")
            contacts_count = CkJsonObject::ckStringOf(jResp,"collection[i].groups[j].contacts_count")
            j = j + 1
        Wend
        i = i + 1
    Wend


    CkHttp::ckDispose(http)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jResp)
    CkDtObj::ckDispose(date_created)
    CkDtObj::ckDispose(date_updated)


    ProcedureReturn
EndProcedure