How to call posts from PHP

I have a website, that uses WP Super Cache plugin. I need to recycle cache once a day and then I need to call 5 posts (URL adresses) so WP Super Cache put these posts into cache again (caching is quite time consuming so I’d like to have it precached before users come so they dont have to wait).

On my hosting I can use a CRON but only for 1 call/hour. And I need to call 5 different URL’s at once.

Read More

Is it possible to do that? Maybe create one HTML page with these 5 posts in iframe? Will something like that work?

Edit: Shell is not available, so I have to use PHP scripting.

Related posts

3 comments

  1. The easiest way to do it in PHP is to use file_get_contents() (fopen() also works), if the HTTP stream wrapper is enabled on your server:

    <?php
    $postUrls = array(
        'http://my.site.here/post1',
        'http://my.site.here/post2',
        'http://my.site.here/post3',
        'http://my.site.here/post4',
        'http://my.site.here/post5',
    );
    
    foreach ($postUrls as $url) {
        // Get the post as an user will do it
        $text = file_get_contents();
        // Here you can check if the request was successful
        // For example, use strpos() or regex to find a piece of text you expect
        // to find in the post
    
        // Replace 'copyright bla, bla, bla' with a piece of text you display
        // in the footer of your site
        if (strpos($text, 'copyright bla, bla, bla') === FALSE) {
            echo('Retrieval of '.$url." failed.n");
        }
    }
    

    If file_get_contents() fails to open the URLs on your server (some ISP restrict this behaviour) you can try to use curl:

    function curl_get_contents($url)
    {
        $ch = curl_init($url);
        curl_setopt_array($ch, array(
            CURLOPT_CONNECTTIMEOUT => 30,        // timeout in seconds
            CURLOPT_RETURNTRANSFER => TRUE,      // tell curl to return the page content instead of just TRUE/FALSE
        ));
    
        $text = curl_exec($ch);
        curl_close($ch);
    
        return $text;  
    }
    

    Then use the function curl_get_contents() listed above instead of file_get_contents().

  2. An example using PHP without building a cURL request.

    Using PHP’s shell exec, you can have an extremely light function like so :

    $siteList = array("http://url1", "http://url2", "http://url3", "http://url4", "http://url5");
    
    foreach ($siteList as &$site) {
        $request = shell_exec('wget '.$site);
    }
    

    Now of course this is not the most concise answer and not always a good solution also, if you actually want anything from the response you will have to work with it a different way to cURLbut its a low impact option.

  3. Thanks to Arkascha tip I created a PHP page that I call from CRON. This page contains simple function using cURL:

    function cache_it($Url){
    
      if (!function_exists('curl_init')){
         die('No cURL, sorry!');
      }
    
      $ch = curl_init();
    
      curl_setopt($ch, CURLOPT_URL, $Url);
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_TIMEOUT, 50); //higher timeout needed for cache to load
    
      curl_exec($ch); //dont need it as output, otherwise $output = curl_exec($ch);
    
      curl_close($ch);
    }
    
    cache_it('http://www.mywebsite.com/url1');
    cache_it('http://www.mywebsite.com/url2');
    cache_it('http://www.mywebsite.com/url3');
    cache_it('http://www.mywebsite.com/url4');
    

Comments are closed.