XF 2.2 Using Guzzle requestAsync?

Dannymh

Active member
Hi all,

With some code that calls API's in my site, I wanted to move to using Guzzle requestAsync instead of request. This is because some of this runs at post time it can cause a delay in the post being completed because of the API requests, so the end user sees the results sometimes after say 15 seconds.

I wanted to see if there was a way to use async so that the request is pushed out but the user making the post doesn't have to wait for the reply to complete their actions as the API calls are external post calls and we don't need the response.

PHP:
$client = \XF::app()->http()->client();
                    $promise = $client->requestAsync('POST', $options->stcc_apiend."posts", [
                        'headers' => [
                            'XF-Api-Key' => $options->stcc_apikey,
                            'XF-Api-User' => $options->stcc_extuser,
                            'Accept' => 'application/json'
                        ],
                        'form_params' => [
                            'thread_id' => $options->stcc_extthread,
                            'message' => $newMessage
                             ],
                              'http_errors' => false
                        ]);

                    $promise->then(function ($response) {
                        echo 'Got a response! ' . $response->getStatusCode();
                    });

This doesn't seem to be executing and I am not sure why, I am guessing that the $promise procedures are not being seen
 
That's not how php works unfortunately. Once the response is returned to the user, the script execution is terminated. Guzzles async is meant to allow you to run multiple concurrent requests without having to wait for each other if they don't really on each other, but you'll still have to wait for them all to finish before you end your execution, otherwise they'll be interrupted.

That said, there is a few hacks available that will allow you to continue script execution after a response has been passed back to the user, see here, but I wouldn't really recommend it.
 
That's not how php works unfortunately. Once the response is returned to the user, the script execution is terminated. Guzzles async is meant to allow you to run multiple concurrent requests without having to wait for each other if they don't really on each other, but you'll still have to wait for them all to finish before you end your execution, otherwise they'll be interrupted.

That said, there is a few hacks available that will allow you to continue script execution after a response has been passed back to the user, see here, but I wouldn't really recommend it.
thanks.

I managed to get the guzzle async in place and it has eeked out slightly better performance for now. I think overall I need to just change the responsibility away from the users post completion to somewhere like a cron task.

In 9 out of 10 requests this is fine so far, just one of the requests seems to be a little slow. It's a fairly small task so it probably has more to do with the response times of the remote API than anything else
 
Storing it locally and then sending it at a later date where you're not dependant on having to reply to the user is definitely the better way. With a server cron, you can send it almost instantly without having to rely on a user ending the connection.
 
Storing it locally and then sending it at a later date where you're not dependant on having to reply to the user is definitely the better way. With a server cron, you can send it almost instantly without having to rely on a user ending the connection.
Yes I think thats where I am going to end up with this.
Its basically a system that uses the xenforo API to send posts between 3 forums into a thread, the server cron is probably ultimately the best performance option, but relies on the other admins being able to configure that cron as well. Not sure the impact of making it a xenforo cron though
 
Back
Top Bottom