Link to home
Create AccountLog in
Avatar of MOSTAGHASSI
MOSTAGHASSIFlag for United States of America

asked on

Extract xml file from php file for RSS.

Hello,

I have  create rss.php file with php+mysql for RSS feed ,now i want that extract the rss.xml file from rss.php for publish on search engines ,please help me that how can i do this?
My codes are as below.
<?php
header('Content-Type: application/rss+xml');
error_reporting(E_ALL);
ini_set('display_errors', 1); 
?>
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">  
<channel>  
<title>My Website Name</title>  
<description>A test of the feed</description>  
<link>The URL to the website</link> 

<?php


//connect to the database and check the connection
$mysqli = new mysqli("$dbhostmag", "$dbusermag", "$dbpassmag", "magazine");
if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); }
$mysqli->set_charset("utf8");
//run the query and check it's all OK.
$query = "
	SELECT tbarticles.articleid, tbarticles.title, tbarticles.state, substr(tbarticles.articlebody,1,200) as description1, tbphotos.photofilename, tbarticles.deletitem, tbarticlecategories.catid, tbcategories.parentid, tbarticles.mediastate	 
	FROM tbarticles 
	INNER JOIN tbphotos ON tbphotos.articleid = tbarticles.articleid 
	INNER JOIN tbarticlecategories ON tbarticlecategories.articleid = tbarticles.articleid
	INNER JOIN tbcategories ON tbcategories.catid = tbarticlecategories.catid
	WHERE tbarticles.deletitem='1'
	ORDER BY tbarticles.articleid DESC
	LIMIT 60";

$result = $mysqli->query($query);
if (!$result) die("Error");






//loop through the results and output them
while ($article = $result->fetch_array()): 

?>
<?php
$parentid = $article["parentid"];

switch ($parentid) {
	case "48":
	case "49":
	case "99":
	case "107":
	case "108":
		$folder='tech';
		break;	

	case "1":
	case "2":
	case "3":
		$folder='artandculture';
		break;
		
		case "53":
   case "54":
   case "56":
   case "59":
   $folder='lifestyle';
	break;
	
   case "63":
   case "65":
   case "67":
   case "68":
   case "69":
   case "70":
   $folder='health';
	break;
	
	case "132":
    $folder='social';
	break;
	

	}
	?>

	<item>
		<title ><?php echo $article["title"]; ?></title>
		
		<link ><?php printf("http://localhost/website/%s/details.php?articleid=%s&parentid=%s&catid=%s</link>", $folder, $article["articleid"], $article["parentid"], $article["catid"])  ?></link>
		<?php echo '<br></br>';?>
		<description><?php echo $article["description1"] ?></description>
		<?php echo '<br></br>';?>
	</item>
<?php endwhile; ?>

</channel>
</rss>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of MOSTAGHASSI

ASKER

Hi ~Ray

I have tested it (my codes),and it seems that is ok,but please let me know that  you have deleted the first line:
header('Content-Type: application/rss+xml');

and at the end the  ob_get_clean() is ok? i didn't find it in php manual .

And where must put the xml file (my_rss.xml) ?

thanks
Yes, the header() call is not needed if writing the file to disk.

Man page: http://php.net/manual/en/function.ob-get-clean.php

And you can put the file anywhere on your server that makes sense for your application.
Ok, but i have a bit confused,i want that i save xml file on my pc local,i executed the codes and in source code i see that xml part is missing and it show that the function could capture xml but ,how access to my_rss.xml ?

I have a warning :

file_put_contents(my_rss.xml): failed to open stream: Permission denied in C:\inetpub\wwwroot\website\General pages\rss1.php on line 276
I don't think I can help with your file permissions from here, sorry.  How do you write other files from your PHP scripts?
I have solved that problem, and now have  access to my_rss.xml,but it is not contain the xml
while when i run .php file it is ok and the xml structure is there.
Please post a link to the PHP script and post the current source code here in the code snippet, thanks.
I try that remove some small error and after i will send.
Hello,
Sorry, i thought i selected A ,if it is possible i can change it.

Thanks
You can ask a moderator to change it -- use the request attention button up near the original question.  And if you still have questions or need help with this, please post back and I'll be glad to work through it.  Best, ~Ray
I use that yellow button ,but there is not box that i post my request,please send me its url.
I can't even find the Request Attention button any more on my view of the page.  Another mystery in the "improved" EE design that was rolled out the last time.
Ok, whenever the problem removed please let me know that i correct it via a request to moderator .
10-4, thanks.
Hi ~Ray

Please let me know that is it possible that you reopen the question so i correct the grade.

Thanks
I have been informed by the moderators that the little yellow triangle is the "Request Attention" button.  EE moves it around (for what purpose, I cannot possibly know) and today it seems to be located at the left of the original question title.  You might try that.  If it doesn't work, don't waste time worrying about it.  Thanks for trying, ~Ray