使用 PHP 类创建 RSS 文件

发布于 2024-08-16 19:23:57 字数 3038 浏览 1 评论 0原文

我在正确执行此脚本时遇到一些困难。

当调用远程函数 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

世界如花海般美丽 2024-08-23 19:23:57

您可能想从带有 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.

从此见与不见 2024-08-23 19:23:57

我的直觉是你遗漏了构造函数。你的函数:

function updateService() {
....
}

可能应该读成:(

function __construct() {
....
}

在 php 中,构造函数与其类同名,请使用神奇的名称 __construct 代替)

(顺便说一句 - 我没有'不要阅读所有代码,如果您在格式化代码方面付出更多努力以使其更易于阅读,您可能会得到更多/更好的回复)

My hunch is that you left out the constructor. Your function:

function updateService() {
....
}

Should probably read:

function __construct() {
....
}

(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)

握住你手 2024-08-23 19:23:57

我可能是错的,但我相信从远程函数“updateStatus”调用私有函数“create_rss”不会返回任何内容,因为文件写入代码生成了错误。

当我将代码分离到它自己的远程函数中时,它返回“未定义”。为了清理代码,我简单地编写了一个返回 true 的条件。这是代码末尾的一个片段:

// SET RSS FILE VARIABLE
//linux    : doc root = dirname
//windows  : doc root = dirname/

$rss_feed_dir = $_SERVER['DOCUMENT_ROOT'] . '/feed/';

chmod($rss_feed_dir, 0777);

$file = $rss_feed_dir . $site_name . '.xml';

$file_handle = fopen($file, "w");
fwrite($file_handle, $xml);
fclose($file_handle);

return 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:

// SET RSS FILE VARIABLE
//linux    : doc root = dirname
//windows  : doc root = dirname/

$rss_feed_dir = $_SERVER['DOCUMENT_ROOT'] . '/feed/';

chmod($rss_feed_dir, 0777);

$file = $rss_feed_dir . $site_name . '.xml';

$file_handle = fopen($file, "w");
fwrite($file_handle, $xml);
fclose($file_handle);

return true;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文