Guest User

Untitled

a guest
Oct 22nd, 2011
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.93 KB | None | 0 0
  1. <?php
  2. //Curl stuff.
  3. $CurlHandler = curl_init("http://api.reddit.com");
  4. curl_setopt($CurlHandler, CURLOPT_HEADER, false); //
  5. curl_setopt($CurlHandler, CURLOPT_RETURNTRANSFER, true);
  6. $JSON_text = curl_exec($CurlHandler);
  7. curl_close($CurlHandler);
  8. $Data = json_decode($JSON_text,true);
  9.  
  10. 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.
  11. ?>
  12. <html>
  13.     <head>
  14.         <title>Reddit api / frontpage fetcher</title>
  15.         <style>
  16.             body { width:1000px;margin: 10px auto; }
  17.             h1 { font-family: 'Helvetica LT Std'; color:gray;  margin:0px;}
  18.             .posts { }
  19.             .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);}
  20.             .posts .post .islink { background: #FFE8E8; color:#D44;}
  21.             .posts .post .isimg { background: #CDDDFC; color: #5b61d3; }
  22.             .posts .post .isself { background: #FFFFC6; color: #a1a13f;}
  23.             .posts .post { margin-top:10px; height:50px; padding-left:60px; }
  24.             .posts .post .title { text-decoration:none;font-family: 'Delicious'; color: #3838e8; font-size: 1em;display:block;}
  25.             .posts .post .info { font-family: Tahoma; font-size:.6em; color:#aaa; text-transform: uppercase; }
  26.             .posts .post .info a { text-decoration:none; color: #abc;}
  27.         </style>
  28.         <link rel="stylesheet" href="fancybox/jquery.fancybox-1.3.4.css" type="text/css" media="screen" />
  29.         <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
  30.         <script type="text/javascript" src="fancybox/jquery.fancybox-1.3.4.pack.js"></script>
  31.         <script type="text/javascript">
  32.             $(document).ready(function(){
  33.                 $('.isimg').siblings('.title').fancybox();
  34.             });
  35.         </script>
  36.     </head>
  37.     <body>
  38.         <h1>Redd.it</h1>
  39.         <div class="posts">
  40. <?php
  41. //Parsing data.
  42. foreach($Data['data']['children'] as $Post) {
  43.     $Post=$Post['data'];   
  44.     //if(strlen($Post['title']) > 200) { continue; } // I don't like links with long titles.
  45.     //The following is a feature: the background of the score and its text  color changes depending on the type of submission.
  46.     //The type can be: self post, link, or image. The latter is displayed in a fancybox.
  47.     if(strpos($Post['url'], '/comments/') > 0) { $BackgroundClass = 'isself'; }
  48.     else if(preg_match('/(\.jpg|\.jpeg|\.gif|\.jpeg|\.tiff)/i',strrchr($Post['url'],'.')) == 1) { $BackgroundClass='isimg'; }
  49.     else { $BackgroundClass='islink';}
  50.    
  51.     print '<div class="post">';
  52.     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>";
  53.     print '</div>';
  54. }
  55. ?>
  56.     </div>
  57.     </body>
  58. </html>
  59.  
Add Comment
Please, Sign In to add comment