John Davidson

php - How can I make a http client request after another is completed?

0 comments
Message:


In my Symfony function I am creating a folder, and inside this folder another folder:


$client = HttpClient::create();

$createFolder = $client->request('MKCOL', $path.$foldername, [
'auth_basic' => [$user, $authKey],
]);


$createImageFolder = $client->request('MKCOL', $path.$foldername/images', [
'auth_basic' => [$user, $authKey],
]);

It works well, but somtimes the first folder is not created fast enough, and the image folder cannot be created. Is there a way, that the second request can wait, until the first folder is created?



My Answer:
Yes, you can use the `wait()` method provided by Symfony's HttpClient to wait for the first request to complete before making the second request. Here's an example of how you can modify your code to achieve this:

php
$client = HttpClient::create();

$createFolder = $client->request('MKCOL', $path.$foldername, [
'auth_basic' => [$user, $authKey],
]);

$createFolder->wait();

$createImageFolder = $client->request('MKCOL', $path.$foldername.'/images', [
'auth_basic' => [$user, $authKey],
]);


By calling the `wait()` method on the `$createFolder` request, the script will wait for the first request to complete before proceeding to make the second request. This should ensure that the second request is only made after the first folder has been successfully created.

Rate this post

4 of 5 based on 5183 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us