"Technology OR #technology OR Tech OR #tech OR Gadgets OR #Mobile OR #apps OR #technews OR Tablets OR #EdTech OR #smartphones", "n" => "20", // Return 20 results "expand" => "true", // Provide full URL details // "g" => "40.72251,-74.020128,100km",// Within 100km of New York Airport ); $results = searchAPIRequest($parameters); // Print some stats. printf("%d links found %s oeembed data\n", count($results['links']), isset($results['links'][0]['expanded']) ? "with" : "without"); printf("%d hashtags found\n", count($results['hashtags'])); printf("%d sources found\n", count($results['sources'])); printf("%d phrases found\n", count($results['phrases'])); // Dump the results to standard output print_r($results); } catch (Exception $e) { echo "Error: " . $e->getMessage() . PHP_EOL; } // End of mainline exit; // Helper functions // Function to call the Trendspottr API via CURL, and check for // authentication and curl errors. function searchAPIRequest ($parameters) { $serverURL = 'http://api.trendspottr.com/v1.5/search?'; $apiKey = 'ENTER_YOUR_API_KEY_HERE'; $validateSSL = false; // Add the API Key to the list of provided parameters $parameters['key'] = $apiKey; // Format the full url we are going to call $url = $serverURL . http_build_query($parameters); // Initialize the PHP curl agent $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); $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), true); if (($error = get_json_error()) !== false) { throw new Exception("json_decode failed: " . $error); } if (isset($result['error'])) { // Failed throw new Exception($result['error']['code'] . ": " . $result['error']['errstring'] . PHP_EOL); } // We got a valid result, verify that. if (!isset($result['results'])) { throw new Exception("json_decode did not return 'results'"); exit; } // The API call succeeded, return results return $result['results']; } // Function to provide informative error message if the // json decode fails - should never happen, but ... 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'; } ?>