Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- // Set up the Reddit API credentials
- $client_id = 'mysecret';
- $client_secret = 'mysecret';
- // Set up the Reddit API endpoint based on the mode parameter
- $subreddit = 'ufc';
- // Check if the subreddit is specified in the query string
- if (isset($_GET['subreddit'])) {
- $subreddit = $_GET['subreddit'];
- }
- $mode = isset($_GET['mode']) ? $_GET['mode'] : 'new';
- if ($mode == 'new') {
- $endpoint = "https://www.reddit.com/r/$subreddit/new.json?limit=25";
- } else if ($mode == 'hot') {
- $endpoint = "https://www.reddit.com/r/$subreddit/hot.json?limit=25";
- } else {
- die("Invalid mode");
- }
- // Set up the cURL request
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $endpoint);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_USERAGENT, 'fightsToday:v1.0 (by /u/futurepr0n)');
- // Execute the cURL request
- $response = curl_exec($ch);
- // Check for cURL errors
- if(curl_errno($ch)) {
- die('Curl error: ' . curl_error($ch));
- }
- curl_close($ch);
- // Decode the response as JSON
- $data = json_decode($response);
- // Create a list of posts
- $posts = array();
- if (isset($data->data) && isset($data->data->children)) {
- $posts = $data->data->children;
- }
- // Create an empty array to hold the post data
- $post_data = array();
- // Loop through each post and extract the data
- // Loop through each post and extract the data
- foreach ($posts as $post) {
- $title = $post->data->title;
- $url = $post->data->url;
- $permalink = $post->data->permalink;
- $num_comments = $post->data->num_comments;
- $selftext = $post->data->selftext;
- $thumbnail = $post->data->thumbnail;
- // Extract YouTube video ID if the URL is a YouTube video
- if (strpos($url, 'youtube.com/watch') !== false) {
- // Extract video ID from YouTube URL in format: https://www.youtube.com/watch?v=l8clIAdBsE0
- preg_match('/[?&]v=([^&]+)/', $url, $matches);
- $video_id = $matches[1];
- $player_id = 'player-' . $video_id;
- $html = "<div id=\"player-$video_id\"></div>";
- $html .= "<script>function onYouTubeIframeAPIReady() {new YT.Player('player-$video_id', {height: '390', width: '640', videoId: '$video_id', events: {'onReady': onPlayerReady,'onStateChange': onPlayerStateChange}});}</script>";
- } else if (strpos($url, 'youtu.be/') !== false) {
- // Extract video ID from YouTube URL in format: https://youtu.be/gaLR-7zJ0HM
- $url_parts = explode('/', $url);
- $video_id = end($url_parts);
- $player_id = 'player-' . $video_id;
- $html = "<div id=\"player-$video_id\"></div>";
- $html .= "<script>function onYouTubeIframeAPIReady() {new YT.Player('player-$video_id', {height: '390', width: '640', videoId: '$video_id', events: {'onReady': onPlayerReady,'onStateChange': onPlayerStateChange}});}</script>";
- } else if (strpos($url, 'youtube.com/shorts/') !== false) {
- // Extract video ID from YouTube URL in format: https://www.youtube.com/shorts/_vYtQCgPICI
- $url_parts = explode('/', $url);
- $video_id = end($url_parts);
- $player_id = 'player-' . $video_id;
- $html = "<div id=\"player-$video_id\"></div>";
- $html .= "<script>function onYouTubeIframeAPIReady() {new YT.Player('player-$video_id', {height: '390', width: '640', videoId: '$video_id', events: {'onReady': onPlayerReady,'onStateChange': onPlayerStateChange}});}</script>";
- } else {
- $video_id = null;
- $player_id = null;
- }
- // Determine the type of post based on the extension of the URL
- $extension = pathinfo($url, PATHINFO_EXTENSION);
- if (in_array($extension, array('jpg', 'jpeg', 'png', 'gif', 'gifv'))) {
- // If the post is an image, create an img element
- $html = "<img src=\"$url\">";
- } else {
- // If the post is not an image, create an a element
- $html = "<a href=\"$url\">$title</a>";
- }
- // Add the post data to the array
- $post_data[] = array(
- 'title' => $title,
- 'url' => $url,
- 'html' => $html,
- 'permalink' => $permalink,
- 'num_comments' => $num_comments,
- 'video_id' => $video_id, // Store the extracted video ID
- 'player_id' => $player_id, // Store the unique player ID
- 'selftext' => $selftext,
- 'thumbnail' => $thumbnail
- );
- }
- // Return the post data as JSON
- header('Content-Type: application/json');
- echo json_encode($post_data);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment