PureBasic
PureBasic
SNS Subscribe SMS Phone Number
See more Amazon SNS Examples
Subscribe the phone number of an SMS-enabled device to an SNS topic.See SNS Subscribe for more information.
Chilkat PureBasic Downloads
IncludeFile "CkXml.pb"
IncludeFile "CkAuthAws.pb"
IncludeFile "CkRest.pb"
Procedure ChilkatExample()
success.i = 0
; This example requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
rest.i = CkRest::ckCreate()
If rest.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Connect to the Amazon AWS REST server.
; such as https://sns.us-west-2.amazonaws.com/
bTls.i = 1
port.i = 443
bAutoReconnect.i = 1
success = CkRest::ckConnect(rest,"sns.us-west-2.amazonaws.com",port,bTls,bAutoReconnect)
; Provide AWS credentials for the REST call.
authAws.i = CkAuthAws::ckCreate()
If authAws.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkAuthAws::setCkAccessKey(authAws, "AWS_ACCESS_KEY")
CkAuthAws::setCkSecretKey(authAws, "AWS_SECRET_KEY")
; the region should match our URL above..
CkAuthAws::setCkRegion(authAws, "us-west-2")
CkAuthAws::setCkServiceName(authAws, "sns")
CkRest::ckSetAuthAws(rest,authAws)
CkRest::ckAddQueryParam(rest,"Action","Subscribe")
; Set the Endpoint to the phone number in E.164 format
CkRest::ckAddQueryParam(rest,"Endpoint","+15552581871")
CkRest::ckAddQueryParam(rest,"Protocol","sms")
CkRest::ckAddQueryParam(rest,"TopicArn","arn:aws:sns:us-west-2:957491831129:chilkat")
responseXml.s = CkRest::ckFullRequestNoBody(rest,"GET","/")
If CkRest::ckLastMethodSuccess(rest) <> 1
Debug CkRest::ckLastErrorText(rest)
CkRest::ckDispose(rest)
CkAuthAws::ckDispose(authAws)
ProcedureReturn
EndIf
; A successful response will have a status code equal to 200.
If CkRest::ckResponseStatusCode(rest) <> 200
Debug "response status code = " + Str(CkRest::ckResponseStatusCode(rest))
Debug "response status text = " + CkRest::ckResponseStatusText(rest)
Debug "response header: " + CkRest::ckResponseHeader(rest)
Debug "response body: " + responseXml
CkRest::ckDispose(rest)
CkAuthAws::ckDispose(authAws)
ProcedureReturn
EndIf
; Examine the successful XML response.
xml.i = CkXml::ckCreate()
If xml.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkXml::ckLoadXml(xml,responseXml)
Debug CkXml::ckGetXml(xml)
; To get the SubscriptionArn
Debug "SubscriptionArn: " + CkXml::ckChilkatPath(xml,"SubscribeResult|SubscriptionArn|*")
; A sample successful response:
; <?xml version="1.0" encoding="utf-8" ?>
; <SubscribeResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/">
; <SubscribeResult>
; <SubscriptionArn>arn:aws:sns:us-west-2:957491831129:chilkat:7eb142f7-2554-4200-a175-bb399588972f</SubscriptionArn>
; </SubscribeResult>
; <ResponseMetadata>
; <RequestId>ceaf538b-1daf-5580-b73f-b347e3733932</RequestId>
; </ResponseMetadata>
; </SubscribeResponse>
CkRest::ckDispose(rest)
CkAuthAws::ckDispose(authAws)
CkXml::ckDispose(xml)
ProcedureReturn
EndProcedure