How to remove error Yandex disk API?

function createDirectory($path) 
{
 $diskClient = new DiskClient(ACCESS_TOKEN);
$diskClient->setServiceScheme(DiskClient::HTTPS_SCHEME);

 $dirContent = $diskClient->createDirectory($path);

 if ($dirContent) return 'Created new directory "' . $path;
}
echo createDirectory('тест55');


If this folder does not exist, then all is well and it will create, but if not, it will come out
Fatal error: Uncaught Yandex\Disk\Exception\DiskRequestException: Service responded with error code: "405" and message: "Method Not Allowed" in /yandex/libs/nixsolutions/yandex-php-library/src/Yandex/Disk/DiskClient.php:109 Stack trace: #0 /yandex/libs/nixsolutions/yandex-php-library/src/Yandex/Disk/DiskClient.php(126): Yandex\Disk\DiskClient->sendRequest('MKCOL', '\xD1\x82\xD0\xB5\xD1\x81\xD1\x8255') #1 /yandex/yandex.functions.php(81): Yandex\Disk\DiskClient->createDirectory('\xD1\x82\xD0\xB5\xD1\x81\xD1\x8255') #2 /yandex/yandex.disk.php(45): createDirectory('\xD1\x82\xD0\xB5\xD1\x81\xD1\x8255') #3 {main} thrown in /yandex/libs/nixsolutions/yandex-php-library/src/Yandex/Disk/DiskClient.php on line 109

and I would like that you wrote about this already existing folder. Tried to check for already exist in this folder, but never came out.
July 2nd 19 at 14:09
1 answer
July 2nd 19 at 14:11
Solution
function createDirectory($path)
{
 $diskClient = new DiskClient(ACCESS_TOKEN);
$diskClient->setServiceScheme(DiskClient::HTTPS_SCHEME);

 try {
 $dirContent = $diskClient->createDirectory($path);

 if ($dirContent) {
 return 'Created new directory "' . $path;
}
 } catch (\Yandex\Disk\Exception\DiskRequestException $e) {
 return "Directory {$path} already exists";
}
}

echo createDirectory('тест55');
Thank you, helped very much! I understand to use the try and catch is necessary because it gives an error? It's just that usually I have such problems solved simple if else - Therese65 commented on July 2nd 19 at 14:14

Find more questions by tags PHPYandex.DiskYandex