doubiaokai4998 2010-01-03 01:03
浏览 53
已采纳

使用PHP类创建RSS文件

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">' . "
";
 $xml .= "\t\t" . "<channel>" . "
";
 $xml .= "\t\t\t" . "<title>Website Feed</title>" . "
";
 $xml .= "\t\t\t" . "<link>http://website.com</link>" . "
";
 $xml .= "\t\t\t" . "<description>Website Design</description>" . "
";

 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>" . "
";
     $xml .= "\t\t\t" . "<title>" . $site_name . " Activity</title>" . "
";
     $xml .= "\t\t\t" . "<link>" . $site_link . "</link>" . "
";
     $xml .= "\t\t\t" . '<description><![CDATA[<p><b>Timestamp: ' . $timestamp . '</b></p>]]>' . "
";  
     $xml .= "\t\t" . "</item>" . "
"; 
 }
 $xml .= "\t" . "</channel>" . "
" . "</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);


    }  
}
?>
  • 写回答

3条回答 默认 最新

  • dseomy1964 2010-01-11 07:39
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改