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
- Create this table in your MySQL database:
CREATE TABLE annNews (last_query INT UNSIGNED NOT NULL, news TEXT NOT NULL );
- Insert the following record in the table you just created:
INSERT INTO annNews VALUES (0, ' ');
- In the code below, replace [mysql_user] by your MySQL username and [mysql_pass] by your MySQL password.
- In the code below, replace [database_name] by the name of your MySQL database
- 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 - Put the code below (with all 5 replacements) in a .php page on your server
- 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']; ?> |