Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed error with promise results #1988

Closed
wants to merge 1 commit into from
Closed

Fixed error with promise results #1988

wants to merge 1 commit into from

Conversation

ThomasLandauer
Copy link

With $results['image']['value'] I was getting:

Cannot use object of type GuzzleHttp\Psr7\Response as array

With `$results['image']['value']` I was getting:

> Cannot use object of type GuzzleHttp\Psr7\Response as array
@gmponos
Copy link
Member

gmponos commented Jul 12, 2020

This is a back and forth situation https://github.com/guzzle/guzzle/pull/2574/files

@GrahamCampbell
Copy link
Member

I suppose I shpuld run the example right now, and find out once and for all...

@GrahamCampbell
Copy link
Member

This PR is incorrect. The current example is correct already.

Copy link
Member

@GrahamCampbell GrahamCampbell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👎

@GrahamCampbell
Copy link
Member

 graham@Grahams-MacBook-Pro  /tmp/guzzle-test  ls              
composer.json composer.lock example.php   vendor
 graham@Grahams-MacBook-Pro  /tmp/guzzle-test  cat composer.json
{
    "name": "root/data",
    "require": {
        "guzzlehttp/guzzle": "^7.0"
    }
}
 graham@Grahams-MacBook-Pro  /tmp/guzzle-test  cat example.php
<?php

require_once __DIR__.'/vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Promise;

$client = new Client(['base_uri' => 'http://httpbin.org/']);

// 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 for the requests to complete; throws a ConnectException
// if any of the requests fail
$responses = Promise\unwrap($promises);

// You can access each response using the key of the promise
echo $responses['image']->getHeader('Content-Length')[0];
echo $responses['png']->getHeader('Content-Length')[0];

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

// Values returned above are wrapped in an array with 2 keys: "state" (either fulfilled or rejected) and "value" (contains the response)
echo $responses['image']['state']; // returns "fulfilled"
echo $responses['image']['value']->getHeader('Content-Length')[0];
echo $responses['png']['value']->getHeader('Content-Length')[0];

 graham@Grahams-MacBook-Pro  /tmp/guzzle-test  php74 example.php 
80908090fulfilled80908090%   

@Nyholm
Copy link
Member

Nyholm commented Sep 20, 2020

This has stalled for a while and it is missing tests.

@GrahamCampbell says that this is incorrect.

I will close this PR now. Feel free to open a new PR with this fix, but make sure to include test that proves the correctness.

@Nyholm Nyholm closed this Sep 20, 2020
@ThomasLandauer ThomasLandauer deleted the patch-1 branch September 20, 2020 12:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants