Το RSS (Really Simple Syndication) είναι πρότυπη μορφή διανομής πληροφοριών. Πολλές ιστοσελίδες παρέχουν RSS feed. Μπορείτε να δημιουργήσετε RSS feed με PHP γράφοντας XML output που ακολουθεί τη μορφή RSS 2.0.
Μελετήστε τη μορφή RSS 2.0. Δημιουργήστε αρχείο PHP.
Δημιουργήστε κλάση RSS channel με title, link, description και array items:
<?php
class RSS_channel {
public $title, $link, $description;
public $items = array();
public function __construct($title, $link, $description) {
$this->title = $title;
$this->link = $link;
$this->description = $description;
}
}
Μέθοδος προσθήκης item:
public function new_item($title, $link, $description) {
array_push($this->items, new RSS_item($title, $link, $description));
}
Μέθοδος header XML/RSS:
public function RSS_header() {
$r = '<?xml version="1.0"?>';
$r .= '<rss version="2.0">';
return $r;
}
Μέθοδος εξόδου channel και items:
public function output() {
$r = $this->RSS_header();
$r .= "<channel>";
$r .= "<title>" . $this->title . "</title>";
$r .= "<link>" . $this->link . "</link>";
$r .= "<description>" . $this->description . "</description>";
foreach ($this->items as $item) {
$r .= $item->output();
}
$r .= "</channel></rss>";
return $r;
}
Δημιουργήστε κλάση RSS_item:
class RSS_item {
public $title, $link, $description;
public function __construct($title, $link, $description) {
$this->title = $title;
$this->link = $link;
$this->description = $description;
}
public function output() {
$r = "<item>";
$r .= "<title>" . $this->title . "</title>";
$r .= "<link>" . $this->link . "</link>";
$r .= "<description>" . $this->description . "</description>";
$r .= "</item>";
return $r;
}
}
Δημιουργήστε channel, προσθέστε items και εκτυπώστε:
$rss = new RSS_channel("News", "https://example.com/news", "Example News");
$rss->new_item("Earnings Report", "https://example.com/earnings", "Earnings Report");
$rss->new_item("Stockholder's Meeting", "https://example.com/shmeeting", "Meeting Announcement");
$rss->new_item("CEO Speech", "https://example.com/ceospeech", "CEO Delivers Speech");
echo $rss->output();
?>
Πνευματικά δικαιώματα © Γνώση Υπολογιστών Όλα τα δικαιώματα διατηρούνται.