×
  • remind me tomorrow
  • remind me next week
  • never remind me
Subscribe to the ANN Newsletter • Wake up every Sunday to a curated list of ANN's most interesting posts of the week. read more

Newsfeed - PHP example

Here is a step-by-step example of how to set up the ANN Newsfeed on your site using the getnews.php script. This example requires that your web host supports PHP and MySQL

  1. Create this table in your MySQL database:
      CREATE TABLE annNews (last_query INT UNSIGNED NOT NULL, news TEXT NOT NULL );
  2. Insert the following record in the table you just created:
      INSERT INTO annNews VALUES (0, ' ');
  3. In the code below, replace [mysql_user] by your MySQL username and [mysql_pass] by your MySQL password.
  4. In the code below, replace [database_name] by the name of your MySQL database
  5. In the code below, replace [u] by your newsfeed username and [p] by your newsfeed password (the getnews password, not the login password). For example, if your username is FOO and your password is BAR, the url should look like
      http://animenewsnetwork.com/newsfeed/getnews.php?u=FOO&p=BAR
  6. Put the code below (with all 5 replacements) in a .php page on your server
  7. If you get a blank page, re-save your settings

<?
// Connect to database
$mysql_connection = mysql_connect("localhost", "[mysql_user]", "[mysql_pass]");
if (!$mysql_connection) die("mysql_connect() failed");
if (!mysql_select_db("[database_name]", $mysql_connection))
die("mysql_select_db() failed");

// Fetch the stored news
$result = mysql_query("SELECT * FROM annNews");
if (!$result) die(mysql_error());
$annNews = mysql_fetch_array($result);

// If the news were last updated more than one hour ago, re-update them
if ($annNews['last_query'] < time() - 3600 or trim($annNews['news']) == "")
{
// Fetch the news from ANN
// You need to put your own username and password here
$a = file("http://animenewsnetwork.com/newsfeed/getnews.php?u=[u]&p=[p]");
$n = addslashes(trim(implode("", $a)));

// Save the news to the database
$query = "UPDATE annNews SET last_query=UNIX_TIMESTAMP()";
if ($n)
{ $query .= ", news='$n'";
$annNews['news'] = stripslashes($n);
}
mysql_query($query);
}

// Display the news
echo $annNews['news'];
?>