public function async_request(array $params, $stdin)
    {
        $this->connect();

        // Pick random number between 1 and max 16 bit unsigned int 65535
        $id = mt_rand(1, (1 << 16) - 1);

        // Using persistent sockets implies you want them keept alive by server!
        $keepAlive = intval($this->_keepAlive || $this->_persistentSocket);

        $request = $this->buildPacket(self::BEGIN_REQUEST
                                     ,chr(0) . chr(self::RESPONDER) . chr($keepAlive) . str_repeat(chr(0), 5)
                                     ,$id
                                     );

        $paramsRequest = '';
        foreach ($params as $key => $value) {
            $paramsRequest .= $this->buildNvpair($key, $value, $id);
        }
        if ($paramsRequest) {
            $request .= $this->buildPacket(self::PARAMS, $paramsRequest, $id);
        }
        $request .= $this->buildPacket(self::PARAMS, '', $id);

        if ($stdin) {
            $request .= $this->buildPacket(self::STDIN, $stdin, $id);
        }
        $request .= $this->buildPacket(self::STDIN, '', $id);

        if (fwrite($this->_sock, $request) === false || fflush($this->_sock) === false) {

            $info = stream_get_meta_data($this->_sock);

            if ($info['timed_out']) {
                throw new TimedOutException('Write timed out');
            }

            // Broken pipe, tear down so future requests might succeed
            fclose($this->_sock);
            throw new \Exception('Failed to write request to socket');
        }

        $this->_requests[$id] = array(
            'state' => self::REQ_STATE_WRITTEN,
            'response' => null
        );

        return $id;
    }
public function async_request(array $params, $stdin)
    {
        $this->connect();

        // Pick random number between 1 and max 16 bit unsigned int 65535
        $id = mt_rand(1, (1 << 16) - 1);

        // Using persistent sockets implies you want them keept alive by server!
        $keepAlive = intval($this->_keepAlive || $this->_persistentSocket);

        $request = $this->buildPacket(self::BEGIN_REQUEST
                                     ,chr(0) . chr(self::RESPONDER) . chr($keepAlive) . str_repeat(chr(0), 5)
                                     ,$id
                                     );

        $paramsRequest = '';
        foreach ($params as $key => $value) {
            $paramsRequest .= $this->buildNvpair($key, $value, $id);
        }
        if ($paramsRequest) {
            $request .= $this->buildPacket(self::PARAMS, $paramsRequest, $id);
        }
        $request .= $this->buildPacket(self::PARAMS, '', $id);

        if ($stdin) {
            $request .= $this->buildPacket(self::STDIN, $stdin, $id);
        }
        $request .= $this->buildPacket(self::STDIN, '', $id);

        if (fwrite($this->_sock, $request) === false || fflush($this->_sock) === false) {

            $info = stream_get_meta_data($this->_sock);

            if ($info['timed_out']) {
                throw new TimedOutException('Write timed out');
            }

            // Broken pipe, tear down so future requests might succeed
            fclose($this->_sock);
            throw new \Exception('Failed to write request to socket');
        }

        $this->_requests[$id] = array(
            'state' => self::REQ_STATE_WRITTEN,
            'response' => null
        );

        return $id;
    }
public function async_request ( array $ params , $ stdin ) { $ this -> connect ( ) ; // Pick random number between 1 and max 16 bit unsigned int 65535 $ id = mt_rand ( 1 , ( 1 << 16 ) - 1 ) ; // Using persistent sockets implies you want them keept alive by server! $ keepAlive = intval ( $ this -> _keepAlive || $ this -> _persistentSocket ) ; $ request = $ this -> buildPacket ( self :: BEGIN_REQUEST , chr ( 0 ) . chr ( self :: RESPONDER ) . chr ( $ keepAlive ) . str_repeat ( chr ( 0 ) , 5 ) , $ id ) ; $ paramsRequest = '' ; foreach ( $ params as $ key => $ value ) { $ paramsRequest .= $ this -> buildNvpair ( $ key , $ value , $ id ) ; } if ( $ paramsRequest ) { $ request .= $ this -> buildPacket ( self :: PARAMS , $ paramsRequest , $ id ) ; } $ request .= $ this -> buildPacket ( self :: PARAMS , '' , $ id ) ; if ( $ stdin ) { $ request .= $ this -> buildPacket ( self :: STDIN , $ stdin , $ id ) ; } $ request .= $ this -> buildPacket ( self :: STDIN , '' , $ id ) ; if ( fwrite ( $ this -> _sock , $ request ) === false || fflush ( $ this -> _sock ) === false ) { $ info = stream_get_meta_data ( $ this -> _sock ) ; if ( $ info [ 'timed_out' ] ) { throw new TimedOutException ( 'Write timed out' ) ; } // Broken pipe, tear down so future requests might succeed fclose ( $ this -> _sock ) ; throw new \ Exception ( 'Failed to write request to socket' ) ; } $ this -> _requests [ $ id ] = array ( 'state' => self :: REQ_STATE_WRITTEN , 'response' => null ) ; return $ id ; }
Execute a request to the FastCGI application asyncronously

This sends request to application and returns the assigned ID for that request.

You should keep this id for later use with wait_for_response(). Ids are chosen randomly
rather than seqentially to guard against false-positives when using persistent sockets.
In that case it is possible that a delayed response to a request made by a previous script
invocation comes back on this socket and is mistaken for response to request made with same ID
during this request.

@param array $params Array of parameters
@param String $stdin Content
@return Integer
Execute a request to the FastCGI application asyncronously