I have a controller like so:
<?php
namespace App\Http\Controllers;
use App\Helpers\LogHelper;
class ExampleController {
public function test() {
$client = new \GuzzleHttp\Client();
LogHelper::logTo('s3_logs', 'starting requests');
$p1 = self::testRequest1($client);
$p2 = self::testRequest2($client);
$p3 = self::testRequest3($client);
return response()->json(['success' => true]);
}
private static function testRequest1($client) {
return $client->requestAsync('GET', 'http://www.zara.com/')
->then(function($response) {
LogHelper::logTo('s3_logs', 'resolved from zara.com');
}, function($e) {
LogHelper::logTo('s3_logs', 'rejected from zara.com');
});
}
private static function testRequest2($client) {
return $client->requestAsync('GET', 'http://www.google.com')
->then(function($response) {
LogHelper::logTo('s3_logs', 'resolved from google.com');
}, function($e) {
LogHelper::logTo('s3_logs', 'rejected from google.com');
});
}
private static function testRequest3($client) {
return $client->requestAsync('GET', 'http://httpbin.org/get')
->then(function($response) {
LogHelper::logTo('s3_logs', 'resolved from httpbin.org');
}, function($e) {
LogHelper::logTo('s3_logs', 'rejected from httpbin.org');
});
}
}
The 'test' method is running just fine and returning the 'success' json exactly correctly, I've triple checked.
But those async requests are not running at all, as far as I can tell. My LogHelper::logTo function (which again runs fine, I'm certain) only logs 'starting requests', not any of the other things.
I tried this as an alternative -- waiting for the promises to resolve, but I don't think laravel likes this, it's erroring when I do it this way:
public function test() {
$client = new \GuzzleHttp\Client();
LogHelper::logTo('s3_logs', 'starting requests');
$p1 = self::testRequest1($client);
$p2 = self::testRequest2($client);
$p3 = self::testRequest3($client);
return $p1->then(function() {
return response()->json(['success' => true]);
});
}
This errors with 'The Response content must be a string or object implementing __toString()'
How can I return my response AFTER a Guzzle promise resolves? And even regardless of returning a response separately, why aren't my async requests running and resolving?