Skip to content

Instantly share code, notes, and snippets.

@aaronpk
Created November 21, 2013 07:59
Show Gist options
  • Select an option

  • Save aaronpk/7577585 to your computer and use it in GitHub Desktop.

Select an option

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/
<?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