Xbase++
Xbase++
Populi Search People
See more Populi Examples
Demonstrates the Populi searchPeople task.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oXml
LOCAL cAccessKey
LOCAL oRest
LOCAL nBAutoReconnect
LOCAL cResponseBody
LOCAL i
LOCAL nCount_i
LOCAL nId
LOCAL cFirst_name
LOCAL cLast_name
LOCAL cMiddle_name
LOCAL cPreferred_name
LOCAL nIs_user
LOCAL cPrimary_email
nSuccess := 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.
oXml := CreateObject("Chilkat.Xml")
nSuccess := oXml:LoadXmlFile("qa_data/tokens/populi_token.xml")
cAccessKey := oXml:GetChildContent("access_key")
IF (oXml:LastMethodSuccess != 1)
? "Did not find the access_key"
oXml:destroy()
RETURN
ENDIF
oRest := CreateObject("Chilkat.Rest")
// 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.
nBAutoReconnect := 1
nSuccess := oRest:Connect("yourcollege.populi.co", 443, 1, nBAutoReconnect)
IF (nSuccess != 1)
? oRest:LastErrorText
oXml:destroy()
oRest:destroy()
RETURN
ENDIF
oRest:Authorization := cAccessKey
oRest:AddQueryParam("task", "searchPeople")
oRest:AddQueryParam("search_term", "Robert")
oRest:AddQueryParam("limit", "10")
cResponseBody := oRest:FullRequestFormUrlEncoded("POST", "/api/index.php")
IF (oRest:LastMethodSuccess != 1)
? oRest:LastErrorText
oXml:destroy()
oRest:destroy()
RETURN
ENDIF
// We should expect a 200 response if successful.
IF (oRest:ResponseStatusCode != 200)
? "Request Header: "
? oRest:LastRequestHeader
? "----"
? "Response StatusCode = " + Str(oRest:ResponseStatusCode)
? "Response StatusLine: " + oRest:ResponseStatusText
? "Response Header:"
? oRest:ResponseHeader
? "Response Body:"
? cResponseBody
oXml:destroy()
oRest:destroy()
RETURN
ENDIF
oXml:LoadXml(cResponseBody)
? oXml:GetXml()
// 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 := 0
nCount_i := oXml:NumChildrenHavingTag("person")
DO WHILE i < nCount_i
oXml:I := i
nId := oXml:GetChildIntValue("person[i]|id")
cFirst_name := oXml:GetChildContent("person[i]|first_name")
cLast_name := oXml:GetChildContent("person[i]|last_name")
cMiddle_name := oXml:GetChildContent("person[i]|middle_name")
cPreferred_name := oXml:GetChildContent("person[i]|preferred_name")
nIs_user := oXml:GetChildIntValue("person[i]|is_user")
cPrimary_email := oXml:GetChildContent("person[i]|primary_email")
i := i + 1
ENDDO
oXml:destroy()
oRest:destroy()