Created
November 21, 2013 07:59
-
-
Save aaronpk/7577585 to your computer and use it in GitHub Desktop.
Sample code for handling a trigger callback from the Geotrigger API. See http://developers.arcgis.com/en/geotrigger-service/api-reference/trigger-create/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $jsonInput = file_get_contents("php://input"); | |
| $input = json_decode($jsonInput); | |
| #file_put_contents('./tmp/'.date('Ymd-His').'.json', $jsonInput); | |
| $device_id = ''; | |
| $client_id = ''; | |
| $client_secret = ''; | |
| // First get an application access token | |
| $ch = curl_init('https://www.arcgis.com/sharing/oauth2/token'); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| curl_setopt($ch, CURLOPT_POST, true); | |
| curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array( | |
| 'grant_type' => 'client_credentials', | |
| 'client_id' => $client_id, | |
| 'client_secret' => $client_secret | |
| ))); | |
| $response = curl_exec($ch); | |
| $token = json_decode($response); | |
| if($token->access_token) { | |
| echo $token->access_token . "\n"; | |
| if($input->trigger && $input->trigger->properties && $input->trigger->properties->title) { | |
| $text = 'Entered ' . $input->trigger->properties->title; | |
| } else { | |
| $text = 'Unknown trigger'; | |
| } | |
| // Send a push notification to the device | |
| $ch = curl_init('https://geotrigger.arcgis.com/device/notify'); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| curl_setopt($ch, CURLOPT_POST, true); | |
| curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array( | |
| 'deviceIds' => $device_id, | |
| 'text' => $text | |
| ))); | |
| curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
| 'Authorization: Bearer '.$token->access_token | |
| )); | |
| $response = curl_exec($ch); | |
| echo $response; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment