簡體   English   中英

不調用PHP流通知回調

[英]PHP Stream Notification Callback Not Invoked

我一直在玩PHP Streams並且一直在嘗試開始編寫這里顯示的類。 至少可以說PHP文檔在這方面有點偏差。

我在獲取流上下文以調用指定的回調方法時遇到了困難。 如果我使用像file_get_contentsfopen這樣的函數連接到套接字,則會調用回調,但如果我使用stream_socket_client則不會。

我認為它應該是因為我將上下文傳遞給stream_socket_client ,如果我使用stream_socket_recvfrom我會從套接字返回相同的字符串,因為fgets會返回。

相關的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

我發現這個有點相關的PHP錯誤報告,但看起來報告毫無意義:

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

從php 5.3.0開始,套接字流看起來不支持此功能。

我能找到的唯一一個調用通知函數(在C代碼中)的函數是main / streams / streams.c中的php_stream_notification_notify。 還有一些#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

歸結為對php_stream_notification_notify的調用。 ftp包裝器例如調用

php_stream_notify_info(context, PHP_STREAM_NOTIFY_CONNECT, NULL, 0);

在php_ftp_fopen_connect中。 與curl和http包裝器相同。 但是沒有對stream_socket_client()或相關函數的調用。 如果用tcp :(甚至文件:)之類的傳輸替換協議包裝器,那么http://php.net/function.stream-notification-callback中的示例將不起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM