Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Curl stuff.
- $CurlHandler = curl_init("http://api.reddit.com");
- curl_setopt($CurlHandler, CURLOPT_HEADER, false); //
- curl_setopt($CurlHandler, CURLOPT_RETURNTRANSFER, true);
- $JSON_text = curl_exec($CurlHandler);
- curl_close($CurlHandler);
- $Data = json_decode($JSON_text,true);
- if(isset($_GET['showdata'])) { header("Content-type: text/plain"); var_dump($Data); exit; } //For dev reasons. append ?showdata to the url to see all the data the api sends.
- ?>
- <html>
- <head>
- <title>Reddit api / frontpage fetcher</title>
- <style>
- body { width:1000px;margin: 10px auto; }
- h1 { font-family: 'Helvetica LT Std'; color:gray; margin:0px;}
- .posts { }
- .posts .post .score {float:left;width:50px;height:35px; padding-top:15px;margin-left:-60px; font-size:.8em; font-family: Tahoma;text-align:center; text-shadow: rgba(255, 255, 255, 0.7);}
- .posts .post .islink { background: #FFE8E8; color:#D44;}
- .posts .post .isimg { background: #CDDDFC; color: #5b61d3; }
- .posts .post .isself { background: #FFFFC6; color: #a1a13f;}
- .posts .post { margin-top:10px; height:50px; padding-left:60px; }
- .posts .post .title { text-decoration:none;font-family: 'Delicious'; color: #3838e8; font-size: 1em;display:block;}
- .posts .post .info { font-family: Tahoma; font-size:.6em; color:#aaa; text-transform: uppercase; }
- .posts .post .info a { text-decoration:none; color: #abc;}
- </style>
- <link rel="stylesheet" href="fancybox/jquery.fancybox-1.3.4.css" type="text/css" media="screen" />
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
- <script type="text/javascript" src="fancybox/jquery.fancybox-1.3.4.pack.js"></script>
- <script type="text/javascript">
- $(document).ready(function(){
- $('.isimg').siblings('.title').fancybox();
- });
- </script>
- </head>
- <body>
- <h1>Redd.it</h1>
- <div class="posts">
- <?php
- //Parsing data.
- foreach($Data['data']['children'] as $Post) {
- $Post=$Post['data'];
- //if(strlen($Post['title']) > 200) { continue; } // I don't like links with long titles.
- //The following is a feature: the background of the score and its text color changes depending on the type of submission.
- //The type can be: self post, link, or image. The latter is displayed in a fancybox.
- if(strpos($Post['url'], '/comments/') > 0) { $BackgroundClass = 'isself'; }
- else if(preg_match('/(\.jpg|\.jpeg|\.gif|\.jpeg|\.tiff)/i',strrchr($Post['url'],'.')) == 1) { $BackgroundClass='isimg'; }
- else { $BackgroundClass='islink';}
- print '<div class="post">';
- print "<span class='score {$BackgroundClass}'>{$Post['score']}</span><a href='{$Post['url']}' class='title'>{$Post['title']}</a> <span class='info'>by <a href='http://reddit.com/user/{$Post['author']}'>{$Post['author']}</a> on {$Post['subreddit']}. <a href='http://reddit.com{$Post['permalink']}'>{$Post['num_comments']} comments</a>.</span>";
- print '</div>';
- }
- ?>
- </div>
- </body>
- </html>
Add Comment
Please, Sign In to add comment