PHP:无限循环和时间限制!

发布于 2024-09-04 05:01:54 字数 342 浏览 6 评论 0原文

我有一段代码,可以通过给定 ID 来获取数据。例如,如果我为其指定 ID 1230,则代码会从网站(外部)获取 ID 为 1230 的文章数据并将其插入到数据库中。

现在的问题是我需要获取所有文章,比如说 ID 00001 到 99999 的文章。 如果执行“for”循环,则 60 秒后 PHP 内部时间限制将停止循环。 如果使用某种 header("Location: code.php?id=00001")header("Location: code.php?id=".$ID) 并增加 $ID++ ,然后重定向到浏览器由于无限循环或重定向问题而阻止我的同一页面。

请帮忙!

I have a piece of code that fetches data by giving it an ID. If I give it an ID of 1230 for example, the code fetches an article data with an ID of 1230 from a web site (external) and insert it into a DB.

Now, the problem is that I need to fetch all the articles, lets say from ID 00001 to 99999.
If a do a 'for' loop, after 60 seconds the PHP internal time limit stops the loop.
If a use some kind of header("Location: code.php?id=00001") or header("Location: code.php?id=".$ID) and increase $ID++ and then redirect to the same page the browser stops me because of the infinite loop or redirection problem.

Please HELP!

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

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

发布评论

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

评论(3

猫弦 2024-09-11 05:01:54

如果您的服务器允许,这可能是最好的解决方案:只需删除此脚本的时间限制即可。

set_time_limit(0);

If your server lets you, this is probably the best solution: just remove the time limit for this script.

set_time_limit(0);
记忆之渊 2024-09-11 05:01:54

有几种方法可以做到这一点。

最好的方法是设置一个 cron 来每 X 分钟执行一次爬虫。

在 sed 中,您需要跟踪您当前的 id。

因此,如果您设置一个函数来写入文件,您可以执行以下操作

打开文件(获取当前 ID)
在 id 处启动解析器 60 次
插入数据
打开文件并使用新 ID 更新它
关闭文件并退出。

这将持续几个小时或无论需要多长时间。

  1. 如果您手动执行此操作,并且每次脚本完成时坐在那里刷新,那么您可以使用会话而不是将 id 写入文件

    `session_start();
    $id = (isset($_SESSION['位置']) ? $_SESSION['位置'] : 0);
    for($i=$id;
    lt;=9999;$i++)
    {
       //获取项目($id); //或者你使用的任何函数!
       //更新下次运行的id。
       $_SESSION['位置'] = $id;
    }`
    
  2. 如果您愿意覆盖服务器资源,您可以使用 set_time_limit(120) 将 60 秒延长 120 秒或您喜欢的任何内容。

Well theres several ways you can do this.

The best way to do this is to set up a cron to execute your scraper every X minutes.

This being sed you will need to keep track of what id your currently at.

so if you set up a function to write to a file you can do the following way

--

Open file (get current id)
Start Parser at the id for 60 times
Insert the data
Open the file and update it with the new id
close files and exit.

This will run over space of few hours or however long it takes.

  1. Is if your doing this manually and your sitting there and refreshing everytime the script finishes then you can use sessions instead of writing the id to the file

    `session_start();
    $id = (isset($_SESSION['position']) ? $_SESSION['position'] : 0);
    for($i=$id;
    lt;=9999;$i++)
    {
       //FetchItem($id); //Or whatever function it is you use!
       //Update the id for next run.
       $_SESSION['position'] = $id;
    }`
    
  2. If your your willing to overide your servers resources you can extend the 60 seconds using set_time_limit(120) for 120 seconds or whatever you prefer.

高跟鞋的旋律 2024-09-11 05:01:54

如果您的服务器不允许您更改脚本时间限制,只需让您的脚本检查数据库中序列中最后插入的文章并从那里开始。

另一种方法:使用 Javascript“window.location =”而不是标头进行重定向。

If your server won't let you change the script time limit, just have your script check the database for the last inserted article in your sequence and start from there.

Another approach: Use Javascript "window.location = " instead of a header to redirect.

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