Trying To Fetch The Latest Reddit Post With The Reddit API

谁说我不能喝 提交于 2021-01-28 01:25:10

问题


I am attempting to scrape an url from reddit and post it on my blog every 30 minutes. This is my current solution:

  1. I created a php file named 'newpost.php' that fetches content from the reddit's api and posts it to my blog. (Every time the page is loaded it fetches/posts the newest reddit post). Here is the code:

    $json = file_get_contents("https://www.reddit.com/r/all/new.json?limit=1");
    $obj = json_decode($json);
    $url = $obj->data->children[0]->data->url;
    // the rest posts this url to my blog
    
  2. I then created a cron job that runs this code every 30 minutes

    wget --no-cache --spider http://mywebsite.com/blogposter/newpost.php
    

My problem is that when the cron job runs it's 2nd time (after 1 hour) it is posting the same url as the first time. This only happens with the cron job. When I manually refresh my page in a browser every couple of minutes it posts a different url every time.

I think something is getting cached, but I'm not sure what. Any suggestions?

来源:https://stackoverflow.com/questions/31443924/trying-to-fetch-the-latest-reddit-post-with-the-reddit-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!