= date_sub( now( ) , INTERVAL 1 HOUR ) "); if (!$result) { die('Error checking cache: ' . mysql_error()); } if (mysql_num_rows($result) == 0) { $refresh = 1; } } if ($refresh) { if (!mysql_query("DELETE FROM bills")) { die('Error cleaning data: ' . mysql_error()); } $insert_statement = "INSERT INTO `bills` (". "`number`, `year`, `url`, `current_chamber`,". "`status`, `date_introduced`, `outcome`, `title`,". "`summary`, `text`, `tags`, `patron_name`,". "`patron_id`, `my_notes`) VALUES ". "('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')"; // get bills $jsonUrl = "http://api.richmondsunlight.com/1.0/photosynthesis/" . $api_key . ".json"; $json = file_get_contents($jsonUrl,0,null,null); $bills = json_decode($json,true); //print_r($bills); if ($bills['error']) { echo($bills['error']['message'] . "
\n" . $bills['error']['details'] . "
\n"); } else { $billCount = count($bills); for ($i = 0; $i < $billCount; $i++) { // [number] => HB2197 [year] => 2011 [notes] => [url] => http://www.richmondsunlight.com/bill/2011/hb2197/ // echo "number: " . $bills[$i]['number'] . "
\n"; // echo "year: " . $bills[$i]['year'] . "
\n"; // echo "url: " . $bills[$i]['url'] . "
\n"; // echo "notes: " . $bills[$i]['notes'] . "
\n"; // to get more info on the bill // http://api.richmondsunlight.com/1.0/bill/YEAR/BILLNUMBER.json $jsonUrl = "http://api.richmondsunlight.com/1.0/bill/".$bills[$i]['year']."/".strtolower($bills[$i]['number']).".json"; $json = @file_get_contents($jsonUrl,0,null,null); if ($json) { $bill = json_decode($json,true); if (!is_null($bill)) { // echo "number: " . $bill['number'] . "
\n"; // echo "current_chamber: " . $bill['current_chamber'] . "
\n"; // echo "status: " . $bill['status'] . "
\n"; // echo "date_introduced: " . $bill['date_introduced'] . "
\n"; // echo "outcome: " . $bill['outcome'] . "
\n"; // echo "title: " . $bill['title'] . "
\n"; // echo "summary: " . $bill['summary'] . "
\n"; // echo "text: " . $bill['text'] . "
\n"; $tagCount = count($bill['tags']); for ($tagIndex = 0; $tagIndex < $tagCount; $tagIndex++) { // echo "tag: " . $bill['tags'][$tagIndex]['tag'] . "
\n"; if (strlen($tags)) { $tags .= ','; } $tags .= $bill['tags'][$tagIndex]['tag']; } // echo "patron: " . $bill['patron']['name'] . " (" . $bill['patron']['id'] . ")
\n"; $sql = sprintf($insert_statement, mysql_real_escape_string($bills[$i]['number']), mysql_real_escape_string($bills[$i]['year']), mysql_real_escape_string($bills[$i]['url']), mysql_real_escape_string($bill['current_chamber']), mysql_real_escape_string($bill['status']), mysql_real_escape_string($bill['date_introduced']), mysql_real_escape_string($bill['outcome']), mysql_real_escape_string($bill['title']), mysql_real_escape_string($bill['summary']), mysql_real_escape_string($bill['text']), mysql_real_escape_string($tags), mysql_real_escape_string($bill['patron']['name']), mysql_real_escape_string($bill['patron']['id']), mysql_real_escape_string($bills[$i]['notes'])); if (!mysql_query($sql)) { die('Error inserting data: ' . mysql_error()); } // echo "1 record added
\n"; } else { echo "ERROR: Could not retrieve additional information from URL:
$jsonUrl
"; } } else { echo "ERROR: Could not retrieve additional information from URL:
$jsonUrl
"; } } if (!mysql_query("DELETE FROM cache;") || !mysql_query("INSERT INTO cache (last_refresh) VALUES (NOW());")) { die('Error updated timestamp: ' . mysql_error()); } // echo "Data Refreshed
\n"; } } $result = mysql_query("SELECT * FROM bills"); while($row = mysql_fetch_array($result)) { echo "

Bill Number: ". $row['number'] . " of " . $row['year'] . "

\n"; echo "Title: " . $row['title'] . "
\n"; echo "Year: " . $row['year'] . "
\n"; echo "Url: " . $row['url'] . "
\n"; echo "Chamber: " . $row['current_chamber'] . "
\n"; echo "Status: " . $row['status'] . "
\n"; echo "Date: " . $row['date_introduced'] . "
\n"; echo "Outcome: " . $row['outcome'] . "
\n"; echo "Patron: " . $row['patron_name'] . " (" . $row['patron_id'] . ")
\n"; echo "Summary: " . $row['summary'] . "
\n"; // echo $row['text'] . "
\n"; $tags = preg_split("/,/", $row['tags']); foreach($tags as $tag) { echo "tag: " . $tag . "
\n"; } echo "My Notes: " . $row['my_notes'] . "
\n"; echo "
"; } mysql_close($con); echo "Done
\n"; ?>