2

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 ?

1
  • Sorry if my answer mislead you in any way, you can connect to unix sockets in PHP, it just seem you cannot from php-curl. You could either use command-line curl or direct socket connection. Commented Nov 23, 2015 at 11:51

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.