PHP RSS Parser – RSS Reader Class for PHP

Just had to create a quick RSS parser for PHP and thought I'd post my solution. The implementation and class follows.

<?php
$rss = new RSSReader('http://news.google.com/?output=rss');
while ($rss -> hasNext())
	print_r($rss -> next());
?>
<?php
class RSSReader {
	var $xml = null;
	var $pos = 0;
	var $count = 0;
 
	function __construct($feed_url) {
		$this -> load_url($feed_url);
	}
 
	function load_url($feed_url) {
		$this -> load_string(file_get_contents($feed_url));
	}
 
	function load_string($feed_string) {
		$this -> xml = simplexml_load_string(str_replace('content:encoded', 'content_encoded', $feed_string));
		$this -> pos = 0;
		$this -> count = count($this -> xml -> channel -> item);
	}
 
	function get_title() {
		return $this -> xml -> channel -> title;
	}
 
	function get_link() {
		return $this -> xml -> channel -> link;
	}
 
	function get_pubdate() {
		return $this -> xml -> channel -> pubdate;
	}
 
	function hasNext() {
		return $this -> count > $this -> pos;
	}
 
	function next() {
		$obj = $this -> xml -> channel -> item[$this -> pos++];
		return array(
			'title' => (string) $obj -> title,
			'link' => (string) $obj -> link,
			'description' => (string) $obj -> description,
			'content' => (string) $obj -> content_encoded,
			'pubDate' => strtotime($obj -> pubDate),
		);
	}
}
?>

Chrome Extensions: History Button, Extensions Button, Downloads Button

I just created some very simple extensions for Chrome shortly after I taught myself how to create extensions.

As I said, they're VERY simple, but I find them useful, so I figured it wouldn't hurt to share them.

Each extension is a simple lightweight extension that adds a shortcut button to your browser for quick access to your history, extensions, or downloads. It either opens a new tab, or switches to the respective tab if you already have it open in a tab you're not using.

You could nearly accomplish the same thing if you made a bookmark, but this assures you won't open multiple tabs and I like to keep some things separate from the bookmarks bar.

Chrome Extensions Button

Chrome History Button

Chrome Downloads Button

MySQL Select Count Distinct – Counting and Identifying Duplicate Records

Looking for a [relatively] quick way to identify duplicate records in a table and list how many records there are for each duplicate? Look no further.

In my case, I’ve got a table named Searches which tracks queries on one of my sites. I wanted to check the top queries, so I used this SQL statement:

SELECT COUNT(*) AS repetitions, Query
FROM Searches
GROUP BY Query
HAVING repetitions > 1
ORDER BY repetitions DESC;

There you have it. A simple, effective way to count and label duplicate records.

30 Google Wave Invites

Google Wave logoI've got 30 more Google Wave invites to give away. Leave a comment with your email and I'll send them. First come, first serve.

This is actually a nomination and it may take a little while before you get your actual invite.

 

From now on, you must retweet the bitly URL (http://bit.ly/2rwYsG) on twitter and reply to me in order to get an invite.

Example tweet:

@xoise Free Google Wave invites: http://bit.ly/2rwYsG #GoogleWave

Then put a comment in the comment section and don't forget your email (doesn't have to be public).

How to retrive Yahoo Backlinks count with PHP

Here's a simple script to retrieve the approximate number of backlinks Yahoo has for a site. The function and how to call it follows:

function get_yahoo_backlinks_count($url) {
	$appid = ''; // get this from https://developer.apps.yahoo.com/wsregapp/
	$url = "http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=$appid&query=site:$url&results=1";

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_REFERER, 'http://www.yoursite.com/');
	$response = curl_exec($ch);
	curl_close($ch);

	$xml = simplexml_load_string($response);
	return $xml -> attributes() -> totalResultsAvailable;
}

echo get_yahoo_backlinks_count('google.com');

How to retrieve Google Backlinks count with PHP

Here's a simple script to retrieve the approximate number of backlinks Google has for a site. The function and how to call it follows:

function get_google_backlinks_count($url) {
	$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=site:$url"

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_REFERER, 'http://www.yoursite.com/');
	$response = curl_exec($ch);
	curl_close($ch);

	$json = json_decode($response);
	return $json ->; responseData ->; cursor -> estimatedResultCount;
}

echo get_google_backlinks_count('yahoo.com');

How to fix linux not sending mail problem: Sendmail stat=Deferred: Connection refused by [127.0.0.1]

I recently experienced a problem where my server was not sending mail from the PHP mail() function or the command line "mail" function. After looking through /var/log/maillog, I was able to see that the local 127.0.0.1 mail server was refusing the connection. Some Google searching landed me at the following thread, where the third post did the trick:

http://www.howtoforge.com/forums/showthread.php?t=16246

Simple PHP script for unzip function

Here’s a simple PHP function to unzip a ZIP file and retain directory structure – great for use when you can’t use exec(). The function will return false on error and true on success. Call it with the following code:

if (unzip('../mtm.zip'))
	echo 'Cool, it worked!';
else
	echo 'Uh oh, something went wrong.';
function unzip($zipfile) {
	$zip = zip_open($zipfile);
	if ($zip) {
		while ($item = zip_read($zip)) {
			$name = zip_entry_name($item);
			if (substr($name, -1) == '/') {
				if (!file_exists($name)) {
					$dir = mkdir($name);
					if (!$dir) {
						zip_close($zip);
						return false;
					}
				}
			} else {
				if (zip_entry_open($zip, $item, "r")) {
					$f = file_put_contents($name, zip_entry_read($item, zip_entry_filesize($item)));
					zip_entry_close($item);
					if ($f === false) {
						zip_close($zip);
						return false;
					}
				} else {
					zip_close($zip);
					return false;
				}
			}
		}
		zip_close($zip);
		return true;
	}
	return false;
}

How to Duplicate Database Rows in MySQL

If you're looking for a quick, easy way to duplicate a MySQL Row, try the following example:

INSERT INTO Tbl1
		(Col2, Col3, ...)
	SELECT
		(Col2, Col3, ...)
	FROM Tbl1
		WHERE Col1 = 1;

This example assumes you want to duplicate the row where Col1 equals 1. You could change the where clause to anything for that matter. However, if there are more than one rows selected from the SELECT statement, it will INSERT each row, not just one.

Likewise, you could duplicate a whole MySQL table using the following:

INSERT INTO Tbl2
		(Col2, Col3, ...)
	SELECT
		(Col2, Col3, ...)
	FROM Tbl1;