通过 CRON 加载 PHP 页面

发布于 2024-11-06 06:35:15 字数 999 浏览 1 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尐籹人 2024-11-13 06:35:15

cronjob 不会影响浏览器。为了刷新页面,您可以使用 HTML

<META HTTP-EQUIV="refresh" CONTENT="60">

或使用 javascript自动刷新页面

function reloadPage(){
    window.location.reload();
}

setTimeout ( reloadPage, 60000);

A cronjob does not affect the browser. In order to refresh the page you could refresh the page automatically with HTML

<META HTTP-EQUIV="refresh" CONTENT="60">

or use javascript

function reloadPage(){
    window.location.reload();
}

setTimeout ( reloadPage, 60000);
烧了回忆取暖 2024-11-13 06:35:15

当我在浏览器中手动刷新 twitter.php 时,我会得到新结果...目标是设置此过程的自动化,以便网站的访问者每分钟左右看到新的(已处理的)结果...< /p>

您需要将它们保存在某个地方(数据库、文本文件等),然后让 twitter.php 从保存的位置加载它们。

加载 twitter.php 会导致它执行,但通过 cron 运行它不会为其他用户或任何东西预先生成它。您当前所做的只是导致 PHP 脚本运行额外的、无用的时间。

您需要两个脚本:

cron.php:(让 cron 执行这个脚本)

<?php
// this file fetches the json and stores it in a text file
$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);

file_put_contents('result.json', $var);

twitter.php

<?php
// this file now loads the json out of the text file, instead of calling Twitter each time
$var = file_get_contents('result.json');
$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...

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 via cron 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)

<?php
// this file fetches the json and stores it in a text file
$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);

file_put_contents('result.json', $var);

twitter.php:

<?php
// this file now loads the json out of the text file, instead of calling Twitter each time
$var = file_get_contents('result.json');
$obj = json_decode($var, true);
for($i = 0; $i<100; $i++){
  echo $obj['results'][$i]['text']."<br/>";
}
echo "<br/>".sizeof($obj);
雨巷深深 2024-11-13 06:35:15

该页面将加载您告诉它加载的任何内容。如果 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文