在 PHP 中控制不同脚本之间的应用程序流

发布于 2024-08-07 02:36:56 字数 222 浏览 6 评论 0原文

我有一个 php 脚本,它接受 POST 请求作为 Web 服务的侦听器,然后将所有数据处理到两个最终数组, 我正在寻找一种方法来启动第二个脚本,获取那些序列化数组并进行更多处理。 include() 对我来说不会有好处,因为我实际上想在传递数据后“释放”或“结束”第一个脚本,

一如既往地非常感谢您的帮助:)

编辑 - 好吧,看起来队列可能是解决方案!在任何示例或参考之前我从未做过类似的事情吗?

i have a php script that accepts a POST request as a listener to a web service then process all the data to two final arrays,
I'm looking for a way to initiate a second script that GET's those serialized arrays and do some more processing.
include() will not be good for me since i actually want to "free" or "end" the first script after passing the data

your help is much appreciated as always :)

EDIT - OK so looks like queue might be the solution! i never did anything like this before any examples or reference?

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

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

发布评论

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

评论(3

零度° 2024-08-14 02:36:56

需要立即发生吗?否则,您可以设置一个 cronjob,每 X 分钟执行一次。您必须创建某种队列,在其中第一个脚本将“请求”粘贴到第二个脚本。然后 cronjob 处理队列中的请求。

Does it need to happen immediately? Otherwise you could set up a cronjob that does that every X minutes. You'll have to make some kind of queue in which your first script sticks "requests" to the second script. The cronjob then processes the requests in the queue.

纸伞微斜 2024-08-14 02:36:56

您应该养成编写只是函数集合的 php 脚本的习惯(本身没有自动运行脚本)。这样,您可以在您所讨论的脚本顶部包含一个脚本文件,然后调用执行您想要的操作的函数。

例如:

<?php
include('common_functions.php');
$array_1 = whatever_you_do_with_post_values();
$array_2 = other_thing_you_do_with_post_values();
// this function is located in 'common_functions.php'
do_stuff_with_arrays($array_1,$array_2); 
?>

事实上:

只是为了与我所说的保持一致:

<?php
include('common_functions.php');
do_your_stuff();
function do_your_stuff() {
    $array_1 = whatever_you_do_with_post_values();
    $array_2 = other_thing_you_do_with_post_values();
    // this function is located in 'common_functions.php'
    do_stuff_with_arrays($array_1,$array_2); 
}
?>

显然你应该使用更好的函数&变量名,哈哈

You should get into the habit of writing php scripts that are just a collection of functions (no auto-ran scripts, per se). This way you can include a script file at the top of the script your talking about and then call the function that does what you want.

For instance:

<?php
include('common_functions.php');
$array_1 = whatever_you_do_with_post_values();
$array_2 = other_thing_you_do_with_post_values();
// this function is located in 'common_functions.php'
do_stuff_with_arrays($array_1,$array_2); 
?>

In Fact:

Just to be consistent with what I'm saying:

<?php
include('common_functions.php');
do_your_stuff();
function do_your_stuff() {
    $array_1 = whatever_you_do_with_post_values();
    $array_2 = other_thing_you_do_with_post_values();
    // this function is located in 'common_functions.php'
    do_stuff_with_arrays($array_1,$array_2); 
}
?>

Obviously you should use better function & variable names, haha.

琉璃繁缕 2024-08-14 02:36:56

我会在一个请求中完成这一切。它减少了延迟并使整个操作更加高效。

请记住,您可以有一个长时间运行的请求,但仍然为其他请求提供服务。 Apache 将生成另一个 php 进程来处理来自 Web 服务的其他请求,即使第一个请求尚未完成。只要脚本不锁定共享资源(数据库文件等),这就可以正常工作。

也就是说,您应该使用 cURL 来调用第二个脚本。然后发布未序列化的数组。 cUrl 将处理剩下的事情。

I'd do it all in one request. It cuts down on latency and makes the whole operation more efficient.

Remember you can have a long running request, but still service other requests. Apache will just spawn another php process to handle the other request from the webservice even though the first has not completed. As long as the script doesn't lock a shared resource (database file etc) this will work just fine.

That said, you should use cURL to call the second script. then post the unserialized array. cUrl will handle the rest.

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