简体   繁体   English

PHP异步Web服务

[英]PHP Async Web Services

如何使用PHP SOAP Extension对Web服务进行异步调用?

My immediate answer should be: You can't. 我的直接答案应该是:你做不到。
PHP does not have threading abilities that can be used in "userland". PHP没有可以在“userland”中使用的线程功能。

Now if you really want to do it, there are some ways you can go around it: 现在,如果你真的想要这样做,你可以通过一些方法来解决它:

  1. Use the exec functions to spawn another process, in the background, and monitor it through the database/file system or whatever. 使用exec函数在后台生成另一个进程,并通过数据库/文件系统或其他任何方式对其进行监视。
  2. Use the fork function to spawn another process and monitor it through the database/file system or whatever. 使用fork函数生成另一个进程并通过数据库/文件系统或其他任何进程监视它。

Setbacks of these 2 approaches is that you can make it asynchronous but if you want a callback then it's going to be very tricky and not at all trivial. 这两种方法的挫折是你可以使它异步,但如果你想要一个回调,那么它将是非常棘手的,而且根本不是微不足道的。 Well, it ain't even gonna be a callback since you'll not be able to wait for it on the script that makes the async call. 好吧,它甚至不会成为一个回调,因为你无法在进行异步调用的脚本上等待它。 This means that you can only have some kind of monitoring scheme. 这意味着您只能拥有某种监控方案。 I would suggest AJAX. 我建议使用AJAX。

如果你使用curl,它有一组'多'调用,允许并行调用多个服务器......

You'll need to write a SoapServer class that continues processing after the client has disconnected. 您需要编写一个SoapServer类,在客户端断开连接后继续处理。 This article will give you a starting point, but you'll have to wrap something similar up inside a SoapServer class. 本文将为您提供一个起点,但您必须在SoapServer类中包装类似的东西。

It's going to look roughly like this (note: I've not tested this inside of SoapServer, but this gives you an idea) 它看起来大致如此(注意:我没有在SoapServer中测试过这个,但这会给你一个想法)

class NonBlockingSoapServer extends SoapServer
{
    public function handle()
    {
        // this script can run forever
        set_time_limit(0);

        // tell the client the request has finished processing
        header('Location: index.php');  // redirect (optional)
        header('Status: 200');          // status code
        header('Connection: close');    // disconnect

        // clear ob stack 
        @ob_end_clean();

        // continue processing once client disconnects
        ignore_user_abort();

        ob_start();
        /* ------------------------------------------*/
        /* this is where regular request code goes.. */
        $result = parent::handle();
        /* end where regular request code runs..     */
        /* ------------------------------------------*/
        $iSize = ob_get_length();
        header("Content-Length: $iSize");

        // if the session needs to be closed, persist it
        // before closing the connection to avoid race
        // conditions in the case of a redirect above
        session_write_close();

        // send the response payload to the client
        @ob_end_flush();
        flush();

        /* ------------------------------------------*/
        /* code here runs after the client diconnect */
        /* YOUR ASYNC CODE HERE ......               */

        return $result;
    }
}

One way is to use the select() ing method provided by CURL's “multi” package, by extending the SoapClient class and implementing your own __doRequest . 一种方法是使用CURL的“multi”包提供的select()方法,扩展SoapClient类并实现自己的__doRequest

The smallest, working example I've found can be downloaded https://github.com/halonsecurity/sp-enduser/blob/master/inc/soap.php and is used like 我发现的最小的工作示例可以下载https://github.com/halonsecurity/sp-enduser/blob/master/inc/soap.php并且像

$client1 = new SoapClientAsync('some-systems-wsdl', $options);
$client2 = new SoapClientAsync('another-systems-wsdl', $options);
$client1->someFunction($arguments);
$client2->anotherFunction($arguments);
soap_dispatch();
$result1 = $client1->someFunction($arguments);
$result2 = $client1->anotherFunction($arguments);

as described here http://www.halon.se/blogs/making-phps-soap-client-asynchronous/ 如此处所述http://www.halon.se/blogs/making-phps-soap-client-asynchronous/

它可能有帮助,(并行远程过程调用): http//en.dklab.ru/lib/Dklab_SoapClient/

如果您能够在Linux中执行命令行php调用,则可以执行pnctl_fork命令并从分叉子进程调用Web服务。

使用AJAX类型调用来做客户端而不是服务器端。

尝试他们在我的问题中给我的方法: 异步PHP调用?

I don't know why Gustavo was modded down as his is the right answer. 我不知道为什么古斯塔沃被改编了,因为他的答案是正确的。

I use exec to run a shell script written in PHP that contacts google API. 我使用exec来运行用PHP编写的shell脚本,该脚本与google API联系。 I start the script like this: 我像这样启动脚本:

run.php param1=1 param2=2 &> ajax.txt run.php param1 = 1 param2 = 2&> ajax.txt

the last line of run is 最后一行是

echo 'finished' 回声'完成'

then my ajax keeps polling 'ajax.txt' until it finds the process has finished. 然后我的ajax继续轮询'ajax.txt',直到它找到进程完成。

Hacky, but simple (KISS) 哈基,但很简单(KISS)

monk.e.boy monk.e.boy

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

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