开发者

Create a RSS File With a PHP Class

开发者 https://www.devze.com 2022-12-15 02:50 出处:网络
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.

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


    }  
}
?>


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.


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)


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

精彩评论

暂无评论...
验证码 换一张
取 消