Sample code for 30+ languages & platforms
PureBasic

Populi Search People

See more Populi Examples

Demonstrates the Populi searchPeople task.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkRest.pb"
IncludeFile "CkXml.pb"

Procedure ChilkatExample()

    success.i = 0

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

    ; First load the previously obtained API token.
    ; See Get Populi Access Token for sample code showing how to get the API token.
    xml.i = CkXml::ckCreate()
    If xml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkXml::ckLoadXmlFile(xml,"qa_data/tokens/populi_token.xml")
    accessKey.s = CkXml::ckGetChildContent(xml,"access_key")
    If CkXml::ckLastMethodSuccess(xml) <> 1
        Debug "Did not find the access_key"
        CkXml::ckDispose(xml)
        ProcedureReturn
    EndIf

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

    ; Connect using TLS.
    ; A single REST object, once connected, can be used for many Populi REST API calls.
    ; The auto-reconnect indicates that if the already-established HTTPS connection is closed,
    ; then it will be automatically re-established as needed.
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"yourcollege.populi.co",443,1,bAutoReconnect)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkXml::ckDispose(xml)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    CkRest::setCkAuthorization(rest, accessKey)

    CkRest::ckAddQueryParam(rest,"task","searchPeople")
    CkRest::ckAddQueryParam(rest,"search_term","Robert")
    CkRest::ckAddQueryParam(rest,"limit","10")

    responseBody.s = CkRest::ckFullRequestFormUrlEncoded(rest,"POST","/api/index.php")
    If CkRest::ckLastMethodSuccess(rest) <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkXml::ckDispose(xml)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    ; We should expect a 200 response if successful.
    If CkRest::ckResponseStatusCode(rest) <> 200
        Debug "Request Header: "
        Debug CkRest::ckLastRequestHeader(rest)
        Debug "----"
        Debug "Response StatusCode = " + Str(CkRest::ckResponseStatusCode(rest))
        Debug "Response StatusLine: " + CkRest::ckResponseStatusText(rest)
        Debug "Response Header:"
        Debug CkRest::ckResponseHeader(rest)
        Debug "Response Body:"
        Debug responseBody
        CkXml::ckDispose(xml)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    CkXml::ckLoadXml(xml,responseBody)
    Debug CkXml::ckGetXml(xml)

    ; Sample response:
    ; Use this online tool to generate parsing code from sample XML: 
    ; Generate Parsing Code from XML

    ; <?xml version="1.0" encoding="UTF-8"?>
    ;  <response>
    ; 	<person>
    ; 		<id>11111</id>
    ; 		<first_name>Robert</first_name>
    ; 		<last_name>McStudent</last_name>
    ; 		<middle_name>Kensington</middle_name>
    ; 		<preferred_name>Bobby</preferred_name>
    ; 		<is_user>1</is_user>
    ; 		<primary_email>r.mcstudent@myschool.edu</primary_email>
    ; 	</person>
    ; 	<person>
    ; 		<id>2222</id>
    ; 		<first_name>Robert</first_name>
    ; 		<last_name>McBoardmember</last_name>
    ; 		<middle_name/>
    ; 		<preferred_name/>
    ; 		<is_user>0</is_user>
    ; 		<primary_email>robert@gmail.com</primary_email>
    ; 	</person>
    ; </response>

    i.i
    count_i.i
    id.i
    first_name.s
    last_name.s
    middle_name.s
    preferred_name.s
    is_user.i
    primary_email.s

    i = 0
    count_i = CkXml::ckNumChildrenHavingTag(xml,"person")
    While i < count_i
        CkXml::setCkI(xml, i)
        id = CkXml::ckGetChildIntValue(xml,"person[i]|id")
        first_name = CkXml::ckGetChildContent(xml,"person[i]|first_name")
        last_name = CkXml::ckGetChildContent(xml,"person[i]|last_name")
        middle_name = CkXml::ckGetChildContent(xml,"person[i]|middle_name")
        preferred_name = CkXml::ckGetChildContent(xml,"person[i]|preferred_name")
        is_user = CkXml::ckGetChildIntValue(xml,"person[i]|is_user")
        primary_email = CkXml::ckGetChildContent(xml,"person[i]|primary_email")
        i = i + 1
    Wend


    CkXml::ckDispose(xml)
    CkRest::ckDispose(rest)


    ProcedureReturn
EndProcedure