I would like to implement a feed aggregator in my app. basically get link, title, description and date then insert to my database so that i can render the data to my chice. I decided to use simplepie but when I run the page I get this The connection to the server was reset while the page was loading. if anyone has done or know why please help. Here is my code.
public function actionCrawl()
{
Yii::import('application.vendors.*');
require "simplepie/simplepie.php";
$feed = new SimplePie();
$feed->set_feed_url('http://dallas.craigslist.org/res/index.rss');
$feed->set_cache_location($_SERVER['DOCUMENT_ROOT'] . 'applications/cache');
//$feed->set_output_encoding('ISO-8859-1');
// Initialize Simplepie.
$feed->init();
//Now it's time to loop through the parsed items and load them to the database.
foreach($feed->get_items() as $item)
{
$link = $item->get_permalink();
$title = $item->get_title();
$description = $item->get_description();
$date = $item->get_date('Y-m-j g:i:s');
//This next part will check for duplicated items by hashing the content and comparing it to the hashes in the database.
$content_hash = md5($title);
$sql = "SELECT * FROM {{classifieds}} WHERE content_hash = :contentHash";
$num_rows = Yii::app()->db->createCommand($sql)->queryRow();
$num_rows->bindValue(":contentHash", $content_hash, PDO::PARAM_STR);
if ($num_rows > 0) { }
// If this item's hash doesn't match any in the database
else
{
if(isset($date))
{
$sql = "INSERT INTO {{classifieds}} (title, , description, content_hash, url, date)
VALUES(:Title, :Description, :Image, :Link, :Updatetime, :date)";
$command = Yii::app()->db->createCommand($sql);
$command->bindValue(":Title", $title, PDO::PARAM_STR);
$command->bindValue(":Description", $description, PDO::PARAM_STR);
//$command->bindValue(":Image", $image, PDO::PARAM_STR);
$command->bindValue(":Link", $link, PDO::PARAM_STR);
$command->bindValue(":date", $date, PDO::PARAM_STR);
return $command->execute();
}
else
{
$sql = "INSERT INTO {{classifieds}} (title, , description, content_hash, url)
VALUES(:Title, :Description, :Image, :Link)";
$command = Yii::app()->db->createCommand($sql);
$command->bindValue(":Title", $title, PDO::PARAM_STR);
$command->bindValue(":Description", $description, PDO::PARAM_STR);
//$command->bindValue(":Image", $image, PDO::PARAM_STR);
$command->bindValue(":Link", $link, PDO::PARAM_STR);
return $command->execute();
}
}
}
echo 'Crawler finished';
}