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


    }  
}
?>
4

3 回答 3

1

您可能希望从带有打印语句的行中删除 //:您已注释掉您的错误消息。如果您已删除它们,请再次运行该脚本。

于 2010-01-03T09:14:09.847 回答
0

我的预感是你遗漏了构造函数。你的功能:

function updateService() {
....
}

应该是:

function __construct() {
....
}

(在 php 中,构造函数与其类具有相同的名称,__construct而是使用魔术名称)

(顺便说一句 - 我没有阅读你所有的代码,如果你在格式化它以便更容易阅读,你可能会得到更多/更好的回应)

于 2010-01-03T02:06:37.327 回答
0

我可能弄错了,但我相信从远程函数“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;
于 2010-01-11T07:39:14.893 回答