Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何获取和显示来自guzzle请求的图像_Php_Ajax_Laravel_Guzzle - Fatal编程技术网

Php 如何获取和显示来自guzzle请求的图像

Php 如何获取和显示来自guzzle请求的图像,php,ajax,laravel,guzzle,Php,Ajax,Laravel,Guzzle,我正试图显示一个来自一个狂饮请求的图像,一切正常,但我无法显示图像 这是我在尝试获取用户数据时得到的结果 object(GuzzleHttp\Psr7\Response)#427 (6) { ["reasonPhrase":"GuzzleHttp\Psr7\Response":private]=> string(2) "OK" ["statusCode":"GuzzleHttp\Psr7\Response":private]=> int(200) ["headers":"Guzzle

我正试图显示一个来自一个狂饮请求的图像,一切正常,但我无法显示图像

这是我在尝试获取用户数据时得到的结果

object(GuzzleHttp\Psr7\Response)#427 (6) { ["reasonPhrase":"GuzzleHttp\Psr7\Response":private]=> string(2) "OK" ["statusCode":"GuzzleHttp\Psr7\Response":private]=> int(200) ["headers":"GuzzleHttp\Psr7\Response":private]=> array(14) { ["Server"]=> array(1) { [0]=> string(6) "Cowboy" } ["Connection"]=> array(1) { [0]=> string(10) "keep-alive" } ["X-Powered-By"]=> array(1) { [0]=> string(7) "Express" } ["X-Timestamp"]=> array(1) { [0]=> string(13) "1556374181931" } ["Content-Disposition"]=> array(1) { [0]=> string(51) "attachment; filename =profile_img-1556366764744.jpg" } ["X-Sent"]=> array(1) { [0]=> string(4) "true" } ["Accept-Ranges"]=> array(1) { [0]=> string(5) "bytes" } ["Cache-Control"]=> array(1) { [0]=> string(17) "public, max-age=0" } ["Last-Modified"]=> array(1) { [0]=> string(29) "Sat, 27 Apr 2019 12:06:07 GMT" } ["Etag"]=> array(1) { [0]=> string(21) "W/"12d37-16a5eb034ec"" } ["Content-Type"]=> array(1) { [0]=> string(10) "image/jpeg" } ["Content-Length"]=> array(1) { [0]=> string(5) "77111" } ["Date"]=> array(1) { [0]=> string(29) "Sat, 27 Apr 2019 14:09:41 GMT" } ["Via"]=> array(1) { [0]=> string(9) "1.1 vegur" } } ["headerNames":"GuzzleHttp\Psr7\Response":private]=> array(14) { ["server"]=> string(6) "Server" ["connection"]=> string(10) "Connection" ["x-powered-by"]=> string(12) "X-Powered-By" ["x-timestamp"]=> string(11) "X-Timestamp" ["content-disposition"]=> string(19) "Content-Disposition" ["x-sent"]=> string(6) "X-Sent" ["accept-ranges"]=> string(13) "Accept-Ranges" ["cache-control"]=> string(13) "Cache-Control" ["last-modified"]=> string(13) "Last-Modified" ["etag"]=> string(4) "Etag" ["content-type"]=> string(12) "Content-Type" ["content-length"]=> string(14) "Content-Length" ["date"]=> string(4) "Date" ["via"]=> string(3) "Via" } ["protocol":"GuzzleHttp\Psr7\Response":private]=> string(3) "1.1" ["stream":"GuzzleHttp\Psr7\Response":private]=> object(GuzzleHttp\Psr7\Stream)#425 (7) { ["stream":"GuzzleHttp\Psr7\Stream":private]=> resource(350) of type (stream) ["size":"GuzzleHttp\Psr7\Stream":private]=> NULL ["seekable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["readable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["writable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["uri":"GuzzleHttp\Psr7\Stream":private]=> string(10) "php://temp" ["customMetadata":"GuzzleHttp\Psr7\Stream":private]=> array(0) { } } } 

我希望从以下代码示例中检索/显示图像 关键是
回显
结果,以便显示图像

public function promiseGuzzleRequest()
{
    $client = new Client(['base_uri' => 'http://httpbin.org/']); //your domain

    // Initiate each request but do not block
    $promises = [
        'image' => $client->getAsync('/image'),
        'png'   => $client->getAsync('/image/png'),
        'jpeg'  => $client->getAsync('/image/jpeg'),
        'webp'  => $client->getAsync('/image/webp')
    ];

    // Wait on all of the requests to complete. Throws a ConnectException
    // if any of the requests fail
    $results = Promise\unwrap($promises);

    // Wait for the requests to complete, even if some of them fail
    $results = Promise\settle($promises)->wait();

    // You can access each result using the key provided to the unwrap
    // function.
    echo $results['image']['value']->getHeader('Content-Length')[0];
    echo $results['png']['value']->getHeader('Content-Length')[0];

}

希望这有助于

一旦您从guzzle获得图像的响应

$client = new \GuzzleHttp\Client(['base_uri' => 'https://www.example.com']);
$response = $client->request('GET', 'wallpaper/wallpaper-preview.jpg', ['stream' => true]);
然后,您可以将其编码为base64并以一种格式发送。您可以使用php提供的函数

”数据:图像/jpeg;base64,您的-base-64-encodedstring'

示例:
'data:image/gif;base64,R0LGodlhaaewamqaaj2m5ME98GRK1Doyyyybr3PhV++Pe99XO81Y50auc6PBkZEgpzbmt7HJa2I57fS3CqU7XGYgE+GQHVRLJ8TR6QXmQiQiWAF9CFFGNMNQMWHAWNWWGSKPKSRMJQLTOOV69NUYXSKQPOTATATA18RWTR1RTIAQA7'

$body=$response->getBody()->getContents();
$base64=base64_编码($body);
$mime=“image/jpeg”;
$img=('data:'.$mime.'base64'.$base64);
返回“”;
额外的
您可以使用,
$response->getHeader('Content-type')[0]从响应中获取mime类型($mime在上面的代码片段中)

感谢您的回答Rachel Gallen,但我的目标是使用stream从guzzle请求中获取图像。如果您能提供帮助,我将不胜感激这是我下面的代码,请尝试{$result=$this->gClient->request('GET',$url,['headers'=>['Authorization'=>'Bearer'.$this->accessToken],'stream'=>true,]];}catch(\GuzzleHttp\Exception\ClientException$e){$this->geterr=json(解码($e->getResponse()->getBody());返回$this->geterr;}//\GuzzleHttp\Exception\ConnectException检查返回$result;
$client = new \GuzzleHttp\Client(['base_uri' => 'https://www.example.com']);
$response = $client->request('GET', 'wallpaper/wallpaper-preview.jpg', ['stream' => true]);
$body = $response->getBody()->getContents();
$base64 = base64_encode($body);
$mime = "image/jpeg";
$img = ('data:' . $mime . ';base64,' . $base64);
return "<img src=$img alt='ok'>";