简体   繁体   English

不调用PHP流通知回调

[英]PHP Stream Notification Callback Not Invoked

I've been playing around with PHP Streams and have been experimenting by beginning to write the class shown here. 我一直在玩PHP Streams并且一直在尝试开始编写这里显示的类。 The PHP docs are bit lean in this area to say the least. 至少可以说PHP文档在这方面有点偏差。

I'm having a difficult time with getting my stream context to invoke the callback method specified. 我在获取流上下文以调用指定的回调方法时遇到了困难。 If I use a function like file_get_contents or fopen to connect to a socket the callback is invoked, but if I use stream_socket_client it does not. 如果我使用像file_get_contentsfopen这样的函数连接到套接字,则会调用回调,但如果我使用stream_socket_client则不会。

I assume it should because I'm passing the context to stream_socket_client and if I use stream_socket_recvfrom I get the same string back from the socket as fgets would return. 我认为它应该是因为我将上下文传递给stream_socket_client ,如果我使用stream_socket_recvfrom我会从套接字返回相同的字符串,因为fgets会返回。

Relevant PHP docs are linked at the end of the post. 相关的PHP文档在帖子的末尾链接。

class IMAP {

    // Connection Parameters
    private $host;
    private $port;
    private $timeout;

    // Credentials
    private $email;
    private $password;

    private $client;
    private $transcript;

    function __construct($connection, $credentials) {

        // Set Connection Settings
        $this->host = $connection['host'];
        $this->port = $connection['port'];
        $this->timeout = $connection['timeout'];

        // Set Credentials
        $this->email = $credentials['email'];
        $this->password = $credentials['password'];

        // Connect to the IMAP server
        $params = array('notification'=>array($this, 'getLine'));
        $ctx = stream_context_create();
        stream_context_set_params($ctx, $params);
        $this->client = stream_socket_client("tcp://$this->host:$this->port",$errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $ctx);
        stream_socket_sendto($this->client, "a001 NOOP\r\n");

    }

    function getLine($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {
        $args = func_get_args();
        var_dump($args);
    }
}

$connection =  array(
    'host' => 'somehost',
    'port' => 143,
    'timeout' => 10
);

$credentails = array(
    'email' => 'someemail',
    'password' => 'somepassword'
);

$imap = new IMAP($connection, $credentails);

?>

http://us3.php.net/manual/en/function.stream-context-set-params.php http://us3.php.net/manual/en/context.params.php http://us3.php.net/manual/en/function.stream-context-set-params.php http://us3.php.net/manual/en/context.params.php

I found this somewhat related PHP bug report too, but it looks like the report was pointless: 我发现这个有点相关的PHP错误报告,但看起来报告毫无意义:

http://bugs.php.net/bug.php?id=42387&edit=1 http://bugs.php.net/bug.php?id=42387&edit=1

Looks like this isn't supported by the socket streams as of php 5.3.0. 从php 5.3.0开始,套接字流看起来不支持此功能。

The only function I could find that calls the notifier function (in the C code) is php_stream_notification_notify in main/streams/streams.c. 我能找到的唯一一个调用通知函数(在C代码中)的函数是main / streams / streams.c中的php_stream_notification_notify。 There are also some #defines 还有一些#defines

#define php_stream_notify_info
#define php_stream_notify_progress
#define php_stream_notify_progress_init
#define php_stream_notify_progress_increment
#define php_stream_notify_file_size
#define php_stream_notify_error

which boil down to a call to php_stream_notification_notify. 归结为对php_stream_notification_notify的调用。 The ftp wrapper eg calls ftp包装器例如调用

php_stream_notify_info(context, PHP_STREAM_NOTIFY_CONNECT, NULL, 0);

in php_ftp_fopen_connect. 在php_ftp_fopen_connect中。 Same with curl and the http wrapper. 与curl和http包装器相同。 But there's no such call for stream_socket_client() or related functions. 但是没有对stream_socket_client()或相关函数的调用。 And the examples at http://php.net/function.stream-notification-callback don't work if you replace the protocol wrapper by a transport like tcp: (or even file:). 如果用tcp :(甚至文件:)之类的传输替换协议包装器,那么http://php.net/function.stream-notification-callback中的示例将不起作用。

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

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