Question: How to Fetch and Insert RSS FEED into MySQL?
I want to fetch and insert data from an rss feed, in this case kotakus rss feed (kotaku.com/vip.xml) .
Currently: i get an error with PDO method: Parse error: syntax error, unexpected '(' in D:\xampp\htdocs\xml\sql_pdo.php on line 11
I'll show Code for three steps. First to Fetch Data that is currently working. Second is a PDO method to fetch&insert data into mySQL ( not working currently ). Third is same as two but using Curl instead (not working either). Im using PhpMyAdmin and Xampp for the MySQL database. I just want either second or third method or another method to work.. only thing that matters is to get results.
First Code:
<?php
$xml= simplexml_load_file("http://kotaku.com/vip.xml");
foreach($xml->channel->item as $itm){
$title = $itm->title;
$description = $itm->description;
echo $title;
echo $description;
} ?>
Second (PDO)
<?php
$db = new PDO('mysql:host=localhost;dbname=simpledata','root','123456');
$xmldoc = new DOMDocument();
$xmldoc = load('http://kotaku.com/vip.xml');
$xmldata = $xmldoc->getElementsByTagName('channel');
$xmlcount = $xmldata->length;
for ($i=0; $i < $xmlcount; $i++) {
$title = $xmldata->item($i)->getElementsByTagName('title')(0)->childNodes->item(0)->nodeValue;
$description = $xmldata->item($i)->getElementsByTagName('description')(0)->childNodes->item(0)->nodeValue;
$stmt = $db->prepare("insert into kotaku values(?,?)");
$stmt->bindParam(1,$title);
$stmt->bindParam(1,$description);
$stmt->execute();
printf($title);
} ?>
Third (CURL)
<?php
$url="http://kotaku.com/vip.xml"; //need complete
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl-setopt($ch, CURLOPT_URL, $url); //get the url contents
$data = curl_exec ($ch); //execute curl request
curl_close($ch);
$xml = simplexml_load_string($data);
$con=mysql_connect("localhost","root","123456"); //connect to server
mysql_select_db("simpledata", $con) or die (mysql_error()); //select database
foreach ($xml -> item as $row) {
$title = $row -> title;
$description = $row -> description;
// performing sql query
$sql = "INSERT INTO 'kotaku' ('title', 'description')"
. "VALUES ('$title', '$description')";
$result = mysql_query($sql);
if (!$result) {
echo 'MySQL ERROR';
} else {
echo 'SUCCES';
}
?>