parse php อ่ะครับ ถ้าจะยัดข้อมูลที่printลงใน db ด้วยต้องใช้การรับค่าแบบไหนอ่ะครับ
<?php
class cfeed
{
public static function parse($url, $limit=0)
{
if(!function_exists('curl_init')) { die('cURL extension needed!'); }
if(!function_exists('simplexml_load_string')) { die('SimpleXML extension needed!'); }
$limit = (int)$limit;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$s = curl_exec($ch);
curl_close($ch);
$feed = simplexml_load_string($s, 'SimpleXMLElement', LIBXML_NOCDATA|LIBXML_COMPACT);
$items = array();
$i = 0;
if(isset($feed->channel))
{
$feed = $feed->xpath('//item');
foreach($feed as $f)
{
$i++;
if($limit>0 && $i>$limit) { break; }
$item = new stdClass;
$item->title = trim($f->title);
$item->description = trim($f->description);
$item->link = trim($f->link);
$item->date = trim($f->pubDate);
$items[] = $item;
}
}
else
{
$feed = $feed->entry;
foreach($feed as $f)
{
$i++;
if($limit>0 && $i>$limit) { break; }
$item = new stdClass;
$item->title = trim($f->title);
$item->description = trim($f->content);
foreach($f->link as $l)
{
if($l['rel'] == 'alternate')
{
$item->link = trim($l['href']);
break;
}
}
$item->date = trim($f->published);
$items[] = $item;
}
}
return $items;
}
}
ในหน้านี้อ่ะครับจะแก้ตรงquery sql ยังไงอ่ะครับ
<?php
include('include.php');
$objConnect = mysql_connect("localhost","root","localhost") or die("Error Connect to Database");
$objDB = mysql_select_db("localhost");
mysql_query("SET NAMES UTF8");
header('content-type: text/plain');
$URL = 'http://www.nationmultimedia.com/rss/topstories.rss';
$feed = cfeed::parse($URL, 5);
mysql_query("SET NAMES UTF8");
$query = mysql_query("INSERT INTO category VALUES ('','','{$cfeed}','{$row[1]}','test','test','','','','')");
echo $query;
print_r($feed);
$URL = 'http://www.rssthai.com/rssutf8/sport.xml';
$feed = cfeed::parse($URL, 5);
print_r($feed);
$URL = 'http://i3rssnews.blogspot.com/feeds/posts/default';
$feed = cfeed::parse($URL, 5);
print_r($feed);
?>
<?php
class cfeed
{
public static function parse($url, $limit=0)
{
if(!function_exists('curl_init')) { die('cURL extension needed!'); }
if(!function_exists('simplexml_load_string')) { die('SimpleXML extension needed!'); }
$limit = (int)$limit;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$s = curl_exec($ch);
curl_close($ch);
$feed = simplexml_load_string($s, 'SimpleXMLElement', LIBXML_NOCDATA|LIBXML_COMPACT);
$items = array();
$i = 0;
if(isset($feed->channel))
{
$feed = $feed->xpath('//item');
foreach($feed as $f)
{
$i++;
if($limit>0 && $i>$limit) { break; }
$item = new stdClass;
$item->title = trim($f->title);
$item->description = trim($f->description);
$item->link = trim($f->link);
$item->date = trim($f->pubDate);
$items[] = $item;
}
}
else
{
$feed = $feed->entry;
foreach($feed as $f)
{
$i++;
if($limit>0 && $i>$limit) { break; }
$item = new stdClass;
$item->title = trim($f->title);
$item->description = trim($f->content);
foreach($f->link as $l)
{
if($l['rel'] == 'alternate')
{
$item->link = trim($l['href']);
break;
}
}
$item->date = trim($f->published);
$items[] = $item;
}
}
return $items;
}
}
ในหน้านี้อ่ะครับจะแก้ตรงquery sql ยังไงอ่ะครับ
<?php
include('include.php');
$objConnect = mysql_connect("localhost","root","localhost") or die("Error Connect to Database");
$objDB = mysql_select_db("localhost");
mysql_query("SET NAMES UTF8");
header('content-type: text/plain');
$URL = 'http://www.nationmultimedia.com/rss/topstories.rss';
$feed = cfeed::parse($URL, 5);
mysql_query("SET NAMES UTF8");
$query = mysql_query("INSERT INTO category VALUES ('','','{$cfeed}','{$row[1]}','test','test','','','','')");
echo $query;
print_r($feed);
$URL = 'http://www.rssthai.com/rssutf8/sport.xml';
$feed = cfeed::parse($URL, 5);
print_r($feed);
$URL = 'http://i3rssnews.blogspot.com/feeds/posts/default';
$feed = cfeed::parse($URL, 5);
print_r($feed);
?>
ไม่ค่อยเข้าใจคำถาม
ข้อมูล RSS ก็เป็นข้อมูลชนิด text ครับ ดังนั้นเก็บในรูป text ก็ได้ครับ
หรือถ้าขี้เกียจ parse ก็อ่านมันทั้ง RSS และเก็บที่อ่านได้ทั้งหมดเลยก็ได้ครับ เป็น text เช่นกัน
ข้อมูล RSS ก็เป็นข้อมูลชนิด text ครับ ดังนั้นเก็บในรูป text ก็ได้ครับ
หรือถ้าขี้เกียจ parse ก็อ่านมันทั้ง RSS และเก็บที่อ่านได้ทั้งหมดเลยก็ได้ครับ เป็น text เช่นกัน