-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSS.php
More file actions
53 lines (47 loc) · 1.97 KB
/
RSS.php
File metadata and controls
53 lines (47 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>RSS Reader</title>
</head>
<body>
<?php
// Define the RSS feed URL
$rssUrl = "https://www.cshub.com/rss/news";
// Define options for the HTTP context, specifically setting a user agent to mimic a browser request
$options = [
"http" => [
"header" => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3\r\n"
]
];
// Create a stream context with the defined options
$context = stream_context_create($options);
// Attempt to fetch the RSS feed content using the custom context
$rss = file_get_contents($rssUrl, false, $context);
// Check if the fetch was successful
if ($rss !== false) {
// Parse the XML content of the RSS feed
$rssFeed = simplexml_load_string($rss);
// Ensure the feed was parsed successfully
if (!empty($rssFeed)) {
// Iterate through each item in the RSS feed
foreach ($rssFeed->channel->item as $item) {
// Extract title, link, and description from each item, casting them to string to avoid simplexml object issues
$title = (string)$item->title;
$link = (string)$item->link;
$description = (string)$item->description;
// Display the title as a link and the description, ensuring special characters are escaped to avoid XSS vulnerabilities
echo "<div><a href='" . htmlspecialchars($link) . "'>" . htmlspecialchars($title) . "</a></div>";
echo "<div>" . htmlspecialchars($description) . "</div><br>";
}
} else {
// Handle errors in parsing the RSS feed
echo "<p>Failed to parse the RSS feed.</p>";
}
} else {
// Handle errors in fetching the RSS feed
echo "<p>Failed to retrieve the RSS feed.</p>";
}
?>
</body>
</html>