Created
April 8, 2018 15:36
-
-
Save dgoujard/ae12232541cc0d47f130126b29e2cbd9 to your computer and use it in GitHub Desktop.
PHP Guzzle <-> Docker socket connector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//php composer.phar require guzzlehttp/guzzle:~6.0 | |
//php composer.phar require php-http/socket-client | |
//php composer.phar require php-http/message | |
//php composer.phar require php-http/client-common | |
require 'vendor/autoload.php'; | |
use GuzzleHttp\Psr7\Uri; | |
use Http\Client\Common\Plugin\AddHostPlugin; | |
use Http\Client\Common\Plugin\ContentLengthPlugin; | |
use Http\Client\Common\Plugin\DecoderPlugin; | |
use Http\Client\Common\Plugin\ErrorPlugin; | |
use Http\Client\Common\PluginClient; | |
use Http\Message\MessageFactory\GuzzleMessageFactory; | |
use Http\Client\Socket\Client; | |
$socketClient = new Client(new GuzzleMessageFactory, [ | |
'remote_socket' => 'unix:///var/run/docker.sock', | |
]); | |
$httpClient = new PluginClient($socketClient, [ | |
new ErrorPlugin(), | |
new ContentLengthPlugin(), | |
new DecoderPlugin(), | |
new AddHostPlugin(new Uri('http://localhost')), | |
]); | |
$response = $httpClient->sendRequest(new \GuzzleHttp\Psr7\Request("GET","http://localhost/images/json")); | |
echo $response->getStatusCode(); //200 | |
$body = $response->getBody(); | |
echo $body; // Liste des images |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment