【问题标题】:How to connect to unix socket from php如何从 php 连接到 unix 套接字
【发布时间】:2018-06-25 16:41:40
【问题描述】:

如何在 php 中将 HTTP 与 unix 域套接字进行通信。
我正在寻找与此类似的库:requests-unixsocket 但这是用于 python,我需要 php。
如果可能,请提供示例代码。

根据this 答案,curl 有--unix-socket 选项,但this 答案中的 php-curl 不支持此选项。
我找到了Socket Client for PHP HTTP php 库,但不知道如何使用它。
请分享类似的库。
编辑:
来自this 博客,我们可以在php 中以某种方式使用CURLOPT_UNIX_SOCKET_PATH 选项吗?

【问题讨论】:

  • 对不起,如果我的回答以任何方式误导了您,您可以连接到 PHP 中的 unix 套接字,看来您不能从 php-curl 连接。您可以使用命令行 curl 或直接套接字连接。

标签: php sockets http unix


【解决方案1】:

试试 stream_socket_client。

resource stream_socket_client ( string $remote_socket [, int &$errno [, string &$errstr [, float $timeout = ini_get("default_socket_timeout") [, int $flags = STREAM_CLIENT_CONNECT [, resource $context ]]]]] )

【讨论】:

    【解决方案2】:

    我在这里可能会迟到,但这里是使用CURLOPT_UNIX_SOCKET_PATH 的方法。 对于其他访问此问题的用户。

        $socket = '/var/run/docker.sock';
    
        set_time_limit(0);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_UNIX_SOCKET_PATH, $socket);
        curl_setopt($ch, CURLOPT_BUFFERSIZE, 256);
        curl_setopt($ch, CURLOPT_TIMEOUT, 1000000);
    
        $writeFunction = function ($ch, $string) {
            echo $string;
            $length = strlen($string);
            printf("Received %d byte\n", $length);
            flush();
            return $length;
        };
        curl_setopt($ch, CURLOPT_WRITEFUNCTION, $writeFunction);
    
    
        curl_setopt($ch, CURLOPT_URL, "http:/localhost/version");
        curl_exec($ch);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-13
      • 2021-08-24
      • 1970-01-01
      • 2016-09-02
      相关资源
      最近更新 更多