L
L
lbarmen2016-11-25 15:14:31
PHP
lbarmen, 2016-11-25 15:14:31

How to remove Yandex Disk API error?

function createDirectory($path) 
{
  $diskClient = new DiskClient(ACCESS_TOKEN);
  $diskClient->setServiceScheme(DiskClient::HTTPS_SCHEME);
  
  $dirContent = $diskClient->createDirectory($path);
  
  if ($dirContent) return 'Создана новая директория "' . $path;
}
echo createDirectory('тест55');

If such a folder does not exist, then everything is fine and it will be created, but if not, then 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

but I would like to write about an already existing such folder. I tried to check for the existence of such a folder, but so far it has not happened.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Max, 2016-11-25
@lbarmen

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

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

        if ($dirContent) {
            return 'Создана новая директория "' . $path;
        }
    } catch (\Yandex\Disk\Exception\DiskRequestException $e) {
        return "Директория {$path} уже существует";
    }
}

echo createDirectory('тест55');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question