Advertisement
TheKambo

PHP Auto WP RSS Feed URL Rotator

Sep 3rd, 2023
1,248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.56 KB | Source Code | 0 0
  1. Here's an updated PHP script that extracts a random link from an RSS feed and redirects to that link:
  2.  
  3. ```php
  4. <?php
  5. // RSS Feed Reader Code
  6.  
  7. // RSS feed URL
  8. $rssFeedURL = 'http://example.com/wordpress/feed';
  9.  
  10. // Fetch the RSS feed
  11. $rssFeed = simplexml_load_file($rssFeedURL);
  12.  
  13. // Check if the RSS feed was loaded successfully
  14. if ($rssFeed !== false) {
  15.     // Create an array to store the links
  16.     $links = array();
  17.    
  18.     // Loop through each item in the feed
  19.     foreach ($rssFeed->channel->item as $item) {
  20.         // Get the article link
  21.         $articleLink = $item->link;
  22.        
  23.         // Add the link to the array
  24.         $links[] = $articleLink;
  25.     }
  26.    
  27.     // Get a random link from the array
  28.     $randomLink = $links[array_rand($links)];
  29.  
  30.     // Redirect to the random link
  31.     header('Location: ' . $randomLink);
  32.     exit();
  33. } else {
  34.     // Error loading the RSS feed
  35.     echo "Error loading RSS feed.";
  36. }
  37. ?>
  38. ```
  39.  
  40. In this script, the RSS feed is fetched using `simplexml_load_file()`, and each article link is extracted and stored in the `$links` array. Then, a random link is selected using `array_rand()`. Finally, the script redirects the user to the randomly selected link using the `header()` function.
  41.  
  42. Remember to replace `'http://example.com/wordpress/feed'` with the actual RSS feed URL from your WordPress blog.
  43.  
  44. Please note that this script assumes that the RSS feed is in a valid format and that the links are present in the feed's items. Make sure to test the script with your specific RSS feed to ensure it works as expected.
Tags: wordpress WP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement