通过 CRON 加载 PHP 页面
我正在 cpanel 中运行以下命令,
/ramdisk/bin/php5 -q /home#/username/etc/php.ini /home#/username/public_html/sitename/subfolder/twitter.php
它应该显示来自 twitter 的某个关键字的搜索结果。 twitter.php 文件工作得很好...但是我的印象是,设置 cron 作业每分钟执行 php 文件将会用新内容重新加载页面。
这不会发生,因为 twitter.php 保持不变(在我的浏览器中查看时)。
我错过了什么吗?问题可能是什么?
编辑:(我认为问题更多地与“cron 部分”有关)这是 twitter.php:
<?
//searches for tweets mentioning "bieber", prints out....
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://search.twitter.com/search.json?q=bieber&rpp=100' );
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$var = curl_exec($ch);
curl_close($ch);
$obj = json_decode($var, true);
for($i = 0; $i<100; $i++){
echo $obj['results'][$i]['text']."<br/>";
}
echo "<br/>".sizeof($obj);
?>
当我在浏览器中手动刷新 twitter.php 时,我得到新结果......目标是设置此过程的自动化,以便该网站的访问者每分钟左右都会看到新的(已处理的)结果...
编辑:我实际上最终希望每分钟处理一次搜索结果,然后每分钟显示它们...我如何将新值传递给 $var 变量每分钟 twitter.php
I am running the following command in cpanel
/ramdisk/bin/php5 -q /home#/username/etc/php.ini /home#/username/public_html/sitename/subfolder/twitter.php
It is supposed to display search results for a certain keyword from twitter. The twitter.php file works just fine...however my impression was that setting the cron job to execute the php file every minute will reload the page with new content.
This isn't happening as twitter.php just remains the same (when viewed in my browser).
Am i missing something? what could the problem be?
EDIT: (I thought the problem was more with the "cron part") Here's twitter.php:
<?
//searches for tweets mentioning "bieber", prints out....
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://search.twitter.com/search.json?q=bieber&rpp=100' );
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$var = curl_exec($ch);
curl_close($ch);
$obj = json_decode($var, true);
for($i = 0; $i<100; $i++){
echo $obj['results'][$i]['text']."<br/>";
}
echo "<br/>".sizeof($obj);
?>
when i manually refresh twitter.php in the browser i get new results...the goal is to set up an automation of this process so visitors of the site see new (processed) results every minute or so...
EDIT: I actually eventually want to process the search results every minute BEFORE displaying them every minute...how do i pass new values to the $var variable in twitter.php every minute
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
cronjob 不会影响浏览器。为了刷新页面,您可以使用 HTML
或使用 javascript自动刷新页面
A cronjob does not affect the browser. In order to refresh the page you could refresh the page automatically with HTML
or use javascript
您需要将它们保存在某个地方(数据库、文本文件等),然后让
twitter.php
从保存的位置加载它们。加载 twitter.php 会导致它执行,但通过 cron 运行它不会为其他用户或任何东西预先生成它。您当前所做的只是导致 PHP 脚本运行额外的、无用的时间。
您需要两个脚本:
cron.php
:(让 cron 执行这个脚本)twitter.php
:You'll need to save them somewhere (a database, a text file, etc.), then, and have
twitter.php
load them out of that saved location.Loading
twitter.php
will cause it to execute, but having it run viacron
won't pre-generate it for other users or anything. All you're currently doing is causing the PHP script to run extra, useless times.You'll need two scripts:
cron.php
: (have cron execute this one)twitter.php
:该页面将加载您告诉它加载的任何内容。如果 cron 作业以某种方式更新内容,则页面将加载(当有人请求时)新内容。
如果 cron 作业不更新内容,那么看到该页面的任何人都将看到旧内容。
The page will load whatever content you tell it to load. And if the cron job updates the content somehow, then the page will load (when someone requests it) with the new content.
If the cron job does not update the content, then anyone who sees the page will see the old content.