如何在 CRON 中递增 PHP 变量

发布于 2024-11-27 09:25:05 字数 301 浏览 1 评论 0原文

我有一个 simpleXML 文件,该文件是从 REST API 调用生成的,并将通过 cronjob 按计划输入数据库。

API 调用返回的结果限制为每页 10 个,我遇到的问题是更新调用的页码参数以获取下一组结果

$page_number = 1;
$page = $page_number++;

$api_url = "example.com/rest/api?products=new&pageid=$page";

我不确定如何让变量 $page 递增每个 cron,非常感谢任何帮助

I have a simpleXML file that is being generated from a rest api call and will be fed into a database on a scheduled basis via a cronjob.

The results returned from the API calls are limited to 10 per page and the problem I am having is updating the page number parameter for the call to get the next set of results

$page_number = 1;
$page = $page_number++;

$api_url = "example.com/rest/api?products=new&pageid=$page";

I'm not sure how to get the variable $page to increment on each cron, any help is much appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

画尸师 2024-12-04 09:25:05

您可以存储在:

  • File =>查看其他答案。为此,您需要访问文件系统(但我认为您应该拥有这个或继续前进)。
  • Redis(或 Memcached )=> http://redistogo.com 提供免费的 Redis 实例和 predis 非常流行的 PHP 库来连接到 redis。这个解决方案将(几乎总是有效并且非常快。您只需要 socket 即可
  • 。SQL => 您应该使用 PDO 连接到 SQL(如果您的托管提供商提供此功能)。我也认为它应该提供这个或你应该继续前进。

You could store in:

  • File => See other answers. You need access to file-system for this(But I think you should have this or move along).
  • Redis(or Memcached) => http://redistogo.com offers free instance of Redis and predis very popular PHP library to connect to redis. This solution will (almost' always work and will be very fast. You only need socket for this.
  • SQL => You should use PDO to connect to SQL if your hosting provider offers this. Also I think it should offer this or you should move along.
尬尬 2024-12-04 09:25:05

在 /tmp/file.txt 中创建新的空文件

$page_number = file_get_contents("/tmp/file.txt");
if (!$page_number) $page_number = 1;
$page = $page_number + 1;
$f = fopen("/tmp/file.txt", "w+");
fwrite($f, $page);
fclose($f);

$api_url = "example.com/rest/api?products=new&pageid=$page";

make new empty file in /tmp/file.txt

$page_number = file_get_contents("/tmp/file.txt");
if (!$page_number) $page_number = 1;
$page = $page_number + 1;
$f = fopen("/tmp/file.txt", "w+");
fwrite($f, $page);
fclose($f);

$api_url = "example.com/rest/api?products=new&pageid=$page";
忘东忘西忘不掉你 2024-12-04 09:25:05

将数字保存在数据库中或保存为仅包含数字作为内容的文件将是最安全的方法。

Saving the number in a DB Or as a file with only the number as content would be the safest way.

夏末的微笑 2024-12-04 09:25:05
$fname = '/tmp/pager_counter'; // don't use tempnam function here!!!
$fp = fopen($fname, 'w+');
$page = (int)fread($fp, filesize($fname));
if (!$page) {
    $pgae=0;
}
$page++;
fwrite($fp, $page, strlen((string)$page));

$api_url = "example.com/rest/api?products=new&pageid=$page";

您可以轻松地使用任何其他数据库或缓存(MySQLmemcache 等)

$fname = '/tmp/pager_counter'; // don't use tempnam function here!!!
$fp = fopen($fname, 'w+');
$page = (int)fread($fp, filesize($fname));
if (!$page) {
    $pgae=0;
}
$page++;
fwrite($fp, $page, strlen((string)$page));

$api_url = "example.com/rest/api?products=new&pageid=$page";

You could as easily use any another database or cache (MySQL, memcache, etc.)

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