使用 PHP 类创建 RSS 文件
我在正确执行此脚本时遇到一些困难。
当调用远程函数 updateStatus 时,create_rss 函数不会创建 RSS 文件。
<?php
define("DB_HOST", "localhost");
define("DB_USER", "user");
define("DB_PASS", "pass");
define("DB_NAME", "db_test");
class updateService
{
function updateService()
{
$this->methodTable = array(
"updateStatus" => array(
"description" => "Retrieve RSS Info",
"arguments" => array("info"),
"access" => "remote"
),
"create_rss" => array(
"description" => "Create RSS",
"arguments" => array("id"),
"access" => "private"
)
);
//Connect to MySQL and select database
$link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
$db = mysql_select_db(DB_NAME);
}
/**
* Update Status
* @access remote
*/
//$info contains the integer site id...
function updateStatus($info)
{
create_rss(4);
}
function create_rss($id)
{
$xml = '<?xml version="1.0" encoding="ISO-8859-1" ?><rss version="2.0">' . "\r\n";
$xml .= "\t\t" . "<channel>" . "\n\r";
$xml .= "\t\t\t" . "<title>Website Feed</title>" . "\n\r";
$xml .= "\t\t\t" . "<link>http://website.com</link>" . "\n\r";
$xml .= "\t\t\t" . "<description>Website Design</description>" . "\n\r";
switch ($id)
{
case 1:
$site_name = 'MyTestWebsite';
$site_link = 'http://www.website.com';
break;
case 2:
$site_name = 'TestWebsite';
$link = 'http://website.com/?q=1&g=2';
$site_link = htmlspecialchars($link);
break;
default:
break;
}
$sql = "SELECT * FROM table1 WHERE site_id = '$id'
LIMIT 30";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$timestamp = $row['timestamp'];
$xml .= "\t\t" . "<item>" . "\n\r";
$xml .= "\t\t\t" . "<title>" . $site_name . " Activity</title>" . "\n\r";
$xml .= "\t\t\t" . "<link>" . $site_link . "</link>" . "\n\r";
$xml .= "\t\t\t" . '<description><![CDATA[<p><b>Timestamp: ' . $timestamp . '</b></p>]]>' . "\n\r";
$xml .= "\t\t" . "</item>" . "\n\r";
}
$xml .= "\t" . "</channel>" . "\n\r" . "</rss>";
//create xml file
$rssfile_path = 'feed/' . $site_name . '.xml';
chmod($rssfile_path, 0777);
$file = $_SERVER['DOCUMENT_ROOT'] . $rssfile_path;
if (!$file_handle = fopen($file, "w"))
{
//print "<br>Cannot open XML document:<br>";
}
elseif (!fwrite($file_handle, $xml))
{
//print "<br>Cannot write to XML document:<br>";
}
else
{
//print "<br>Successfully created XML document:<br>";
}
fclose($file_handle);
}
}
?>
I'm having some difficulty getting this script to execute properly.
The create_rss function does not create the RSS file when the remote function updateStatus is called.
<?php
define("DB_HOST", "localhost");
define("DB_USER", "user");
define("DB_PASS", "pass");
define("DB_NAME", "db_test");
class updateService
{
function updateService()
{
$this->methodTable = array(
"updateStatus" => array(
"description" => "Retrieve RSS Info",
"arguments" => array("info"),
"access" => "remote"
),
"create_rss" => array(
"description" => "Create RSS",
"arguments" => array("id"),
"access" => "private"
)
);
//Connect to MySQL and select database
$link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
$db = mysql_select_db(DB_NAME);
}
/**
* Update Status
* @access remote
*/
//$info contains the integer site id...
function updateStatus($info)
{
create_rss(4);
}
function create_rss($id)
{
$xml = '<?xml version="1.0" encoding="ISO-8859-1" ?><rss version="2.0">' . "\r\n";
$xml .= "\t\t" . "<channel>" . "\n\r";
$xml .= "\t\t\t" . "<title>Website Feed</title>" . "\n\r";
$xml .= "\t\t\t" . "<link>http://website.com</link>" . "\n\r";
$xml .= "\t\t\t" . "<description>Website Design</description>" . "\n\r";
switch ($id)
{
case 1:
$site_name = 'MyTestWebsite';
$site_link = 'http://www.website.com';
break;
case 2:
$site_name = 'TestWebsite';
$link = 'http://website.com/?q=1&g=2';
$site_link = htmlspecialchars($link);
break;
default:
break;
}
$sql = "SELECT * FROM table1 WHERE site_id = '$id'
LIMIT 30";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$timestamp = $row['timestamp'];
$xml .= "\t\t" . "<item>" . "\n\r";
$xml .= "\t\t\t" . "<title>" . $site_name . " Activity</title>" . "\n\r";
$xml .= "\t\t\t" . "<link>" . $site_link . "</link>" . "\n\r";
$xml .= "\t\t\t" . '<description><![CDATA[<p><b>Timestamp: ' . $timestamp . '</b></p>]]>' . "\n\r";
$xml .= "\t\t" . "</item>" . "\n\r";
}
$xml .= "\t" . "</channel>" . "\n\r" . "</rss>";
//create xml file
$rssfile_path = 'feed/' . $site_name . '.xml';
chmod($rssfile_path, 0777);
$file = $_SERVER['DOCUMENT_ROOT'] . $rssfile_path;
if (!$file_handle = fopen($file, "w"))
{
//print "<br>Cannot open XML document:<br>";
}
elseif (!fwrite($file_handle, $xml))
{
//print "<br>Cannot write to XML document:<br>";
}
else
{
//print "<br>Successfully created XML document:<br>";
}
fclose($file_handle);
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能想从带有 print 语句的行中删除 //:您已经注释掉了错误消息。
如果您已删除它们,请再次运行该脚本。
You might want to remove the // from the lines with print statements: you have commented out your error messages.
If you have removed them, run the script again.
我的直觉是你遗漏了构造函数。你的函数:
可能应该读成:(
在 php 中,构造函数不与其类同名,请使用神奇的名称
__construct
代替)(顺便说一句 - 我没有'不要阅读所有代码,如果您在格式化代码方面付出更多努力以使其更易于阅读,您可能会得到更多/更好的回复)
My hunch is that you left out the constructor. Your function:
Should probably read:
(in php, the constructor does not have the same name as its class, use the magic name
__construct
instead)(BTW - i didn't read all your code, you might get more/bettre responses if you put a bit more effort in formatting it so that it easier to read)
我可能是错的,但我相信从远程函数“updateStatus”调用私有函数“create_rss”不会返回任何内容,因为文件写入代码生成了错误。
当我将代码分离到它自己的远程函数中时,它返回“未定义”。为了清理代码,我简单地编写了一个返回 true 的条件。这是代码末尾的一个片段:
I may be mistaken, but I believe that calling a private function "create_rss" from a remote function "updateStatus" does not return anything because of errors generated by the file writing code.
When I separated the code into its own remote function, it returned "undefined". To clean up the code I simply wrote a conditional returning true. Here's a snippet at the end of the code: