'http://post.foo.com/webhook.php', 'secret' => 'topsecret!!', ); $result = alertsAPIRequest('setWebhook', $parameters); // Read the webhook back $parameters = array( // No parameters to this api method ); $result = alertsAPIRequest('getWebhook', $parameters); echo "webhook set to: {$result->url}, secret: \"{$result->secret}\"\n"; // Create an alert for a user called "user1". The userid parameter can be a number or a string // or any combination of the above that allows you to uniquely identify a customer of yours on whose // behalf you are creating an alert. When alert events are posted, the userid will be include to // identify whose alert triggered. $parameters = array( 'userid' => 'user1', 'query_string' => 'Technology', 'level_filter' => 'All', ); $result = alertsAPIRequest('createAlert', $parameters); // The result from create alerts contains the alertid we will need to manipulate the alert in the future // you must save this value! $alert_id = $result->alert_id; // Now change the alert so only High and Extreme alerts are delivered $parameters = array( 'alert_id' => $alert_id, 'level_filter' => 'HighAndExtreme', ); $result = alertsAPIRequest('setAlertLevel', $parameters); // Pause the alert. It still exists, but no alerts for it will be delivered while it is paused. $parameters = array( 'alert_id' => $alert_id, 'state' => 'paused', ); $result = alertsAPIRequest('setAlertState', $parameters); // Make the active again. $parameters = array( 'alert_id' => $alert_id, 'state' => 'active', ); $result = alertsAPIRequest('setAlertState', $parameters); // Retreive all available data about the alert $parameters = array( 'alert_id' => $alert_id, ); $result = alertsAPIRequest('getAlertInfo', $parameters); echo "getAlertInfo:\n"; print_r($result); // Delete the alert $parameters = array( 'alert_id' => $alert_id, ); $result = alertsAPIRequest('deleteAlert', $parameters); // Try to retreive all available data about the alert, this time call will // fail because the alert has been deleted. $parameters = array( 'alert_id' => $alert_id, ); $result = alertsAPIRequest('getAlertInfo', $parameters); } catch (Exception $e) { echo "Error: " . $e->getMessage() . PHP_EOL; } // End of mainline exit; // Helper functions function alertsAPIRequest ($action, $parameters) { $serverURL = 'https://trendspottr.com/api/v1.5/alerts?'; $apiKey = 'ENTER_YOUR_API_KEY_HERE'; $validateSSL = false; // Add two more parameters to whatever was passed to us, the API Key required // for all calls, and the action. $parameters['key'] = $apiKey; $parameters['action'] = $action; // Format the full url we are going to call $url = $serverURL . http_build_query($parameters); $ch = curl_init(); curl_setopt($ch, CURLOPT_USERAGENT, "curl"); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FAILONERROR, true); curl_setopt($ch, CURLOPT_HTTPGET, true); // curl_setopt($ch, CURLOPT_VERBOSE, true); if ($validateSSL) { // The libcurl included in PHP does not come with any of the certificates needed to // validate SSL connections built in. Before you can validate SSL certificates, you // must download a current version, install it on your system and change the path in // the line below to point to that file. You can find a pre-built version of the // certificates bundle at // http://curl.haxx.se/docs/caextract.html // haxx.se is the group responsible for libcurl. If you don't trust this, you can // get the file used by the current version of Firefox from Mozilla.org, but will be // in the wrong format. You must convert it to .pem format before it can be used by libcurl. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($ch, CURLOPT_CAINFO, "/path/to/ca-bundle.crt"); } else { // Disable SSL verification. This is NOT recommended. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); } $result = curl_exec($ch); if ($result === false) throw new Exception ("curl Error: " . curl_error($ch)); $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($http_status != 200) throw new Exception("Request Failed. http status: " . $http_status); curl_close($ch); // Trim any whitespace from the front and end of the string and decode it $result = json_decode(trim($result)); if (($error = get_json_error()) !== false) { throw new Exception("json_decode failed: " . $error); } // The API request suceeded, now, did the API request succeed or fail? if ($result->status == 'ERROR') { // Failed throw new Exception($action . ': '. $result->error_msg); } // The API call succeeded, return the decoded object return $result; } // Function to provide informative error message if the json decode fails. function get_json_error() { switch (json_last_error()) { case JSON_ERROR_NONE: return false; break; case JSON_ERROR_DEPTH: return 'Maximum stack depth exceeded'; break; case JSON_ERROR_STATE_MISMATCH: return 'Underflow or the modes mismatch'; break; case JSON_ERROR_CTRL_CHAR: return 'Unexpected control character found'; break; case JSON_ERROR_SYNTAX: return 'Syntax error, malformed JSON'; break; case JSON_ERROR_UTF8: return 'Malformed UTF-8 characters, possibly incorrectly encoded'; break; } return 'Unknown error'; }