/
index.php
152 lines (139 loc) · 3.65 KB
/
index.php
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>RSS feed</title>
<style>
body {
background: #FEFEFE;
font-family: arial;
font-size: 12px;
color: #333333;
text-align: center;
}
.container {
width: 940px;
background: #FFF;
border: 1px solid #cccccc;
border-radius: 4px;
text-align: left;
padding: 0 10px;
margin: auto;
}
.content {
text-align: center;
}
.ul_json {
margin: 0px ;
padding: 0px;
}
.ul_json li {
margin: 10px 0px ;
padding: 0px 5px;
list-style: none;
text-align: left;
background: #F2F2F2;
border: 1px solid #cccccc;
}
.ul_json li a {
text-decoration: none;
color: #000;
}
.ul_json li a:hover {
color: #666666;
}
.ul_json li span {
font-size: 18px;
}
</style>
</head>
<body>
<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<p>
<select name="select">
<option value="">-- select RSS</option>
<option value="http://rss.cnn.com/rss/cnn_world.rss">CNN</option>
<option value="http://feeds.reuters.com/Reuters/worldNews">Reuters</option>
<option value="posts.json">Local File</option>
</select>
<input type="submit" value="Select">
</p>
</form>
<?php
$RssClass = new RssClass;
$rssURL = $_POST['select'];
if($rssURL){
$jfo = $RssClass -> RSS($rssURL);
// read the title value
$title = $jfo->channel->title;
$link = $jfo->channel->link;
$description = $jfo->channel->description;
$language = $jfo->channel->language;
$pubDate = $RssClass -> DateFormat($jfo->channel->pubDate);
$image = $jfo->channel->image->url;
// copy the posts array to a php var
$items = $jfo->channel->item;
?>
<div class="container">
<p><img src="<?php echo $image; ?> " width = "100px"></p>
<p>Language: <?php echo $language; ?></p>
<p>Date: <?php echo $pubDate; ?></p>
<p>Title: <?php echo $title; ?></p>
<div class="content">
<ul class="ul_json">
<?php
foreach ($items as $item) {
$title = $item->title;
$description = $RssClass ->trimAds($item->description);
$excerpt = $RssClass ->trimDescription($description);
$date = $RssClass -> DateFormat($item->pubDate);
$link = $item->link;
?>
<li>
<h3><a href="<?php echo $link; ?>" target="_blank"><?php echo $title; ?></a></h3>
<p><?php echo $excerpt; ?></p>
<p><?php if($description){echo $description;}else{echo 'n/a';} ?></p>
<p><?php echo $date; ?></p>
<p><a href="<?php echo $link;?>" target="_blank">[ open link ]</a></p>
</li>
<?php
} // end foreach
?>
</ul>
</div><!-- content -->
</div><!-- container -->
<?php
} //end if
class RssClass{
function DateFormat($TheDate){ //convert date to different format
$NewDate=date("F d, Y", strtotime($TheDate));
return $NewDate;
}
function trimAds($str){ // replace ' with ASCII code '
$str = str_replace("'", ''', $str);
$str_array = explode("clear=", $str);
return $str_array[0];
}
function trimDescription($str){ //trim the description and add 3 dots
$str_array = str_split($str, 30);
return $str_array[0].'...';
}
function RSS($rssURL){ //get the JSON file - if $rssURL start with http will go online if $rssURL start with post then load the local server file
if(substr($rssURL, 0, 4) == 'http'){
$feed = implode(file($rssURL));
$xml = simplexml_load_string($feed);
$json = json_encode($xml);
// convert the string to a json object
$jfo = json_decode($json);
}
if(substr($rssURL, 0, 4) == 'post'){
$json_file = file_get_contents('posts.json');
// convert the string to a json object
$jfo = json_decode($json_file);
}
return $jfo;
}
}
?>
</body>
</html>