如何调用来自 PHP 的帖子
How to call posts from PHP
我有一个使用 WP Super Cache 插件的网站。我需要每天回收一次缓存,然后我需要调用 5 个帖子(URL 地址)所以 WP Super Cache 将这些帖子再次放入缓存(缓存非常耗时所以我想在它之前预缓存用户来了,所以他们不必等待)。
在我的主机上,我可以使用 CRON,但只能用于 1 个 call/hour。我需要同时调用 5 个不同的 URL。
可以吗?也许在 iframe 中用这 5 个帖子创建一个 HTML 页面?这样的东西行得通吗?
编辑:Shell 不可用,所以我必须使用 PHP 脚本。
使用 PHP 而不构建 cURL
请求的示例。
使用 PHP 的 shell exec,你可以有一个非常轻的函数,像这样:
$siteList = array("http://url1", "http://url2", "http://url3", "http://url4", "http://url5");
foreach ($siteList as &$site) {
$request = shell_exec('wget '.$site);
}
当然,这不是最简洁的答案,并不总是一个好的解决方案另外,如果你真的想要从响应中得到任何东西,你将不得不以不同的方式使用它通往 cURL
的方式,但它是一个影响较小的选项。
在 PHP 中最简单的方法是使用 file_get_contents()
(fopen()
also works), if the HTTP stream wrapper 在您的服务器上启用:
<?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");
}
}
如果 file_get_contents()
无法打开您服务器上的 URL(某些 ISP 限制此行为),您可以尝试使用 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;
}
然后使用上面列出的函数curl_get_contents()
代替file_get_contents()
。
感谢 Arkascha 提示,我创建了一个从 CRON 调用的 PHP 页面。此页面包含使用 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');
我有一个使用 WP Super Cache 插件的网站。我需要每天回收一次缓存,然后我需要调用 5 个帖子(URL 地址)所以 WP Super Cache 将这些帖子再次放入缓存(缓存非常耗时所以我想在它之前预缓存用户来了,所以他们不必等待)。
在我的主机上,我可以使用 CRON,但只能用于 1 个 call/hour。我需要同时调用 5 个不同的 URL。
可以吗?也许在 iframe 中用这 5 个帖子创建一个 HTML 页面?这样的东西行得通吗?
编辑:Shell 不可用,所以我必须使用 PHP 脚本。
使用 PHP 而不构建 cURL
请求的示例。
使用 PHP 的 shell exec,你可以有一个非常轻的函数,像这样:
$siteList = array("http://url1", "http://url2", "http://url3", "http://url4", "http://url5");
foreach ($siteList as &$site) {
$request = shell_exec('wget '.$site);
}
当然,这不是最简洁的答案,并不总是一个好的解决方案另外,如果你真的想要从响应中得到任何东西,你将不得不以不同的方式使用它通往 cURL
的方式,但它是一个影响较小的选项。
在 PHP 中最简单的方法是使用 file_get_contents()
(fopen()
also works), if the HTTP stream wrapper 在您的服务器上启用:
<?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");
}
}
如果 file_get_contents()
无法打开您服务器上的 URL(某些 ISP 限制此行为),您可以尝试使用 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;
}
然后使用上面列出的函数curl_get_contents()
代替file_get_contents()
。
感谢 Arkascha 提示,我创建了一个从 CRON 调用的 PHP 页面。此页面包含使用 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');