简体   繁体   English

如何实例化 PHP 对象并传递给 shell_exec?

[英]How to instantiate a PHP object and pass to shell_exec?

I have a PHP process that can take many minutes to complete.我有一个可能需要几分钟才能完成的 PHP 进程。 From what I have been reading on the web, I believe that the correct way to handle this is that I should use PHP shell_exec() to pass this long running task off to "at" and let the long running task complete.从我在网上阅读的内容来看,我认为处理这个问题的正确方法是我应该使用 PHP shell_exec() 将这个长时间运行的任务传递给“at”并让长时间运行的任务完成。 Then once the process is done, write the results to a dbase or file in order to read from the web later on.然后,一旦该过程完成,将结果写入 dbase 或文件,以便稍后从 Web 读取。

However, I need to first get some data from the user via the web in order to continue to on and process the request in the shell script.但是,我需要首先通过网络从用户那里获取一些数据,以便继续处理 shell 脚本中的请求。

Is it possible to instantiate a PHP object via the web session, then pass that object off to the shell_exec() which will run via the "at"?是否可以通过 Web 会话实例化 PHP 对象,然后将该对象传递给将通过“at”运行的 shell_exec()? Or does the Apache/PHP architecture not work like that?还是 Apache/PHP 架构不能那样工作?

If yes, how do you pass a PHP object instantiated in the web session to shell_exec()?如果是,如何将在 Web 会话中实例化的 PHP 对象传递给 shell_exec()?

thanks谢谢

It depends on the object You want to pass.这取决于您要传递的对象。

If the object contains only pure data, You can serialize it.如果对象只包含纯数据,则可以对其进行序列化。 There is pair of functions in PHP: PHP中有两个函数:

  1. https://www.php.net/manual/en/function.serialize.php https://www.php.net/manual/en/function.serialize.php
  2. https://www.php.net/manual/en/function.unserialize.php https://www.php.net/manual/en/function.unserialize.php

The first one creates a string.第一个创建一个字符串。 You can pass this string to any shell command.您可以将此字符串传递给任何 shell 命令。 The second converts this string back to an object.第二个将此字符串转换回对象。

Some objects are too complicated and cannot be easily serialized.有些对象过于复杂,不能轻易序列化。 The PDO is an example of this. PDO 就是一个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM