vaibhavatul47
vaibhavatul47

Reputation: 2885

How to connect to unix socket from php

How can I communicate HTTP to unix domain sockets in php.
I am looking for a library simmilar to this: requests-unixsocket but this is for python, I need for php.
Kindly give example code if possible.

According to this answer curl has --unix-socket option but this is not supported in php-curl from this answer.
I found Socket Client for PHP HTTP library for php but cant figure out how to use it.
Kindly share similar libraries.
Edit:
From this blog, can we somehow use CURLOPT_UNIX_SOCKET_PATH option in php ?

Upvotes: 2

Views: 4633

Answers (2)

aimme
aimme

Reputation: 6763

I might be late here but here is how it's done using CURLOPT_UNIX_SOCKET_PATH. for other users who visit finding an answer for this.

    $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);

Upvotes: 3

edt
edt

Reputation: 92

try 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 ]]]]] )

Upvotes: 1

Related Questions