How to call messages from PHP

I have a website that uses the WP Super Cache plugin. I need to recycle the cache once a day and then I need to call 5 messages (urls) so that WP Super Cache will put those messages in the cache again (caching takes quite a long time, so I would like it to be delivered in front of users so they don't have to wait).

On my hosting, I can use CRON, but only for 1 call / hour. And I need to call 5 different urls at once.

Can this be done? Maybe create one HTML page with these 5 posts in an iframe? Will something like this work?

Edit: the shell is not available, so I have to use PHP scripts.

+3


source to share


3 answers


The easiest way to do this in PHP is to use file_get_contents()

( fopen()

also works) if your server has HTTP stream wrapper enabled :

<?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()

your server urls cannot be opened (some providers restrict this behavior), you can try using 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()

above instead file_get_contents()

.

+2


source


An example of using PHP without creating a request cURL

.

Using PHP shell exec you can have an extremely lightweight function like:



$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 shortest answer, and not always a good solution either, if you really want something from the answer, you will have to work with it differently with cURL

but its low impact option.

+2


source


Thanks to Arkascha's advice, I have created a PHP page that I call from CRON. This page contains a simple cURL function:

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');

      

0


source







All Articles