Guest User

Untitled

a guest
Apr 9th, 2024
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.44 KB | None | 0 0
  1. <?php
  2. session_start();
  3. // Set up the Reddit API credentials
  4. $client_id = 'mysecret';
  5. $client_secret = 'mysecret';
  6.  
  7. // Set up the Reddit API endpoint based on the mode parameter
  8. $subreddit = 'ufc';
  9.  
  10. // Check if the subreddit is specified in the query string
  11. if (isset($_GET['subreddit'])) {
  12.     $subreddit = $_GET['subreddit'];
  13. }
  14.  
  15.  
  16.  
  17. $mode = isset($_GET['mode']) ? $_GET['mode'] : 'new';
  18. if ($mode == 'new') {
  19.     $endpoint = "https://www.reddit.com/r/$subreddit/new.json?limit=25";
  20. } else if ($mode == 'hot') {
  21.     $endpoint = "https://www.reddit.com/r/$subreddit/hot.json?limit=25";
  22. } else {
  23.     die("Invalid mode");
  24. }
  25.  
  26. // Set up the cURL request
  27. $ch = curl_init();
  28. curl_setopt($ch, CURLOPT_URL, $endpoint);
  29. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  30. curl_setopt($ch, CURLOPT_USERAGENT, 'fightsToday:v1.0 (by /u/futurepr0n)');
  31.  
  32. // Execute the cURL request
  33. $response = curl_exec($ch);
  34.  
  35.  
  36. // Check for cURL errors
  37. if(curl_errno($ch)) {
  38.     die('Curl error: ' . curl_error($ch));
  39. }
  40.  
  41.  
  42. curl_close($ch);
  43.  
  44. // Decode the response as JSON
  45. $data = json_decode($response);
  46.  
  47. // Create a list of posts
  48. $posts = array();
  49. if (isset($data->data) && isset($data->data->children)) {
  50.     $posts = $data->data->children;
  51. }
  52.  
  53. // Create an empty array to hold the post data
  54. $post_data = array();
  55.  
  56. // Loop through each post and extract the data
  57. // Loop through each post and extract the data
  58. foreach ($posts as $post) {
  59.     $title = $post->data->title;
  60.     $url = $post->data->url;
  61.     $permalink = $post->data->permalink;
  62.     $num_comments = $post->data->num_comments;
  63.     $selftext = $post->data->selftext;
  64.     $thumbnail = $post->data->thumbnail;
  65.  
  66.     // Extract YouTube video ID if the URL is a YouTube video
  67.     if (strpos($url, 'youtube.com/watch') !== false) {
  68.         // Extract video ID from YouTube URL in format: https://www.youtube.com/watch?v=l8clIAdBsE0
  69.         preg_match('/[?&]v=([^&]+)/', $url, $matches);
  70.         $video_id = $matches[1];
  71.         $player_id = 'player-' . $video_id;
  72.         $html = "<div id=\"player-$video_id\"></div>";
  73.         $html .= "<script>function onYouTubeIframeAPIReady() {new YT.Player('player-$video_id', {height: '390', width: '640', videoId: '$video_id', events: {'onReady': onPlayerReady,'onStateChange': onPlayerStateChange}});}</script>";
  74.     } else if (strpos($url, 'youtu.be/') !== false) {
  75.         // Extract video ID from YouTube URL in format: https://youtu.be/gaLR-7zJ0HM
  76.         $url_parts = explode('/', $url);
  77.         $video_id = end($url_parts);
  78.         $player_id = 'player-' . $video_id;
  79.         $html = "<div id=\"player-$video_id\"></div>";
  80.         $html .= "<script>function onYouTubeIframeAPIReady() {new YT.Player('player-$video_id', {height: '390', width: '640', videoId: '$video_id', events: {'onReady': onPlayerReady,'onStateChange': onPlayerStateChange}});}</script>";
  81.     } else if (strpos($url, 'youtube.com/shorts/') !== false) {
  82.         // Extract video ID from YouTube URL in format: https://www.youtube.com/shorts/_vYtQCgPICI
  83.         $url_parts = explode('/', $url);
  84.         $video_id = end($url_parts);
  85.         $player_id = 'player-' . $video_id;
  86.         $html = "<div id=\"player-$video_id\"></div>";
  87.         $html .= "<script>function onYouTubeIframeAPIReady() {new YT.Player('player-$video_id', {height: '390', width: '640', videoId: '$video_id', events: {'onReady': onPlayerReady,'onStateChange': onPlayerStateChange}});}</script>";
  88.     } else {
  89.         $video_id = null;
  90.         $player_id = null;
  91.     }
  92.    
  93.     // Determine the type of post based on the extension of the URL
  94.     $extension = pathinfo($url, PATHINFO_EXTENSION);
  95.     if (in_array($extension, array('jpg', 'jpeg', 'png', 'gif', 'gifv'))) {
  96.         // If the post is an image, create an img element
  97.         $html = "<img src=\"$url\">";
  98.     } else {
  99.         // If the post is not an image, create an a element
  100.         $html = "<a href=\"$url\">$title</a>";
  101.     }
  102.  
  103.    
  104.     // Add the post data to the array
  105.     $post_data[] = array(
  106.         'title' => $title,
  107.         'url' => $url,
  108.         'html' => $html,
  109.         'permalink' => $permalink,
  110.         'num_comments' => $num_comments,
  111.         'video_id' => $video_id, // Store the extracted video ID
  112.         'player_id' => $player_id, // Store the unique player ID
  113.         'selftext' => $selftext,
  114.         'thumbnail' => $thumbnail
  115.     );
  116. }
  117.  
  118.  
  119. // Return the post data as JSON
  120. header('Content-Type: application/json');
  121. echo json_encode($post_data);
  122. ?>
  123.  
Advertisement
Add Comment
Please, Sign In to add comment