I'm trying to use Guzzle async requests to populate an object's properties based on an api response.
How would I access an object like $myObj
below, inside the response handler to operate on?
As is, $myObj
is not reachable. I did find when working inside a class, $this
is accessible from within the response handler, but I'm hoping there's another way.
$myObj;
$promise = $this->client->requestAsync('GET', 'http://example.com/api/someservice');
$promise->then(
function (ResponseInterface $res) {
$data = json_decode($res->getBody());
// How can I access vars like $myObj from here?
$myObj->setName($data->name);
// ... then persist to db
},
function (RequestException $e) {
}
};