From 3c9a682cc51862bc4d9dac4fffc6038547bd3646 Mon Sep 17 00:00:00 2001 From: nickgs Date: Fri, 26 Apr 2013 16:25:02 -0400 Subject: [PATCH] Refactor startupgenome_get.php to consume Startup Genome API using CURL. --- represent-map/startupgenome_get.php | 53 ++++++++++++----------------- 1 file changed, 21 insertions(+), 32 deletions(-) diff --git a/represent-map/startupgenome_get.php b/represent-map/startupgenome_get.php index ae925b5..cac3ffc 100644 --- a/represent-map/startupgenome_get.php +++ b/represent-map/startupgenome_get.php @@ -14,29 +14,29 @@ $interval_info = mysql_fetch_assoc($interval_query); if((time()-$interval_info[sg_lastupdate]) > $sg_frequency || $_GET['override'] == "true") { - // connect to startup genome API - if(strpos($_SERVER['SERVER_NAME'],'.local') !== false) { - $config = array('api_url' => 'startupgenome.com.local/api/'); - } else { - $config = array('api_url' => 'www.startupgenome.com/api'); - } + //grab the location configuration. We can probably remove this and just use $sg_location directly. $config['search_location'] = $sg_location; - $http = Http::connect($config['api_url'],false,'http'); - try { - $r = $http->doGet("login/{$sg_auth_code}"); - $j = json_decode($r,1); - $http->setHeaders(array("AUTH_CODE: {$sg_auth_code}")); - $user = $j['response']; - } catch(Exception $e) { - $error = "
".print_r($e)."
"; - exit(); - } + + // set the header required by the API. + $headers = array("AUTH-CODE: {$sg_auth_code}"); + + // connect to startup genome API using cURL + $curl = curl_init(); - // get organizations - try { - $r = $http->doGet("/organizations{$config['search_location']}"); - $places_arr = json_decode($r, 1); + // Set some options + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLINFO_HEADER_OUT, true); + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); + curl_setopt($curl, CURLOPT_URL, "http://startupgenome.com/api/organizations{$config['search_location']}"); + // Send the request & save response to $resp + $resp = curl_exec($curl); + + // Close request to clear up some resources + curl_close($curl); + + $places_arr = json_decode($resp, 1); + // update organizations in local db $org_array = Array(); foreach ($places_arr['response'] as $key => $place) { @@ -111,17 +111,6 @@ // update settings table with the timestamp for this sync mysql_query("UPDATE settings SET sg_lastupdate='".time()."'"); - - // show errors if there were any issues - } catch (Exception $e) { - echo "
"; - print_r($e); - echo "
"; - exit(); - } - - - } } @@ -129,4 +118,4 @@ -?> \ No newline at end of file +?>