{"id":350559,"date":"2023-07-20T21:00:15","date_gmt":"2023-07-20T21:00:15","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=350559"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=350559","title":{"rendered":"<span>API \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a (PHP) #6: \u0413\u043e\u0442\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 API \u0447\u0435\u0440\u0435\u0437 Curl<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0417\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u044f \u043e\u0441\u043d\u043e\u0432\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u043a\u0443\u0440\u0441\u0430, \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u0443\u0440\u043e\u043a\u0435 \u043c\u044b \u0441 \u0432\u0430\u043c\u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432 API \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0430. \u041c\u044b \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u043c \u0432\u0435\u0441\u044c \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u0440\u0430\u043d\u0435\u0435 \u043a\u043e\u0434 \u0438 \u043e\u0441\u0442\u0430\u0432\u0438\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b.<\/p>\n<p>\u0417\u043d\u0430\u044f \u043c\u0435\u0442\u043e\u0434\u044b \u0438 URL \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u044f \u0440\u0435\u0448\u0438\u043b \u0443\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043e\u0431\u043e\u043b\u043e\u0447\u043a\u0430\u043c\u0438 \u0438 \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043a \u043c\u0435\u0442\u043e\u0434\u0443 <strong>sendQueryYaDisk()<\/strong>.<\/p>\n<p>\u0422\u0430\u043a \u0436\u0435 \u044f \u0440\u0435\u0448\u0438\u043b \u0441\u0442\u0440\u043e\u043a\u0443 <strong>https:\/\/cloud-api.yandex.net\/v1\/disk<\/strong> \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e <strong>$basicApiUrl<\/strong>, \u0430 \u043c\u0435\u0442\u043e\u0434 <strong>sendQueryYaDisk()<\/strong> \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u0435\u0440\u0432\u044b\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u043d\u0435 \u043f\u043e\u043b\u043d\u044b\u0439 URL, \u0430 \u0435\u0433\u043e \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u0435 \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434\u044b.<\/p>\n<h3>\u041a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code> class Backup {     protected $token = '\u0442\u043e\u043a\u0435\u043d';     protected $basicApiUrl = 'https:\/\/cloud-api.yandex.net\/v1\/disk\/';      \/**      * Method sendQueryYaDisk      *      * @param string $urlQuery URL \u0434\u043b\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432      * @param array $arrQuery \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432      * @param string $methodQuery \u043c\u0435\u0442\u043e\u0434 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438      *      * @return array      *\/     public function sendQueryYaDisk(string $methodAPI = '', array $arrQuery = [], string $methodQuery = 'GET'): array     {         if($methodQuery == 'POST') {             $fullUrlQuery = $this->basicApiUrl . $methodAPI;         } else {             $fullUrlQuery = $this->basicApiUrl . $methodAPI . '?' . http_build_query($arrQuery);         }          $ch = curl_init($fullUrlQuery);         switch ($methodQuery) {             case 'PUT':                 curl_setopt($ch, CURLOPT_PUT, true);                 break;              case 'POST':                 curl_setopt($ch, CURLOPT_POST, 1);             curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($arrQuery));                 break;              case 'DELETE':                 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');                 break;         }          curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: OAuth ' . $this->token]);         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);         curl_setopt($ch, CURLOPT_HEADER, false);         $resultQuery = curl_exec($ch);         curl_close($ch);          return (!empty($resultQuery)) ? json_decode($resultQuery, true) : [];     }            \/**      * \u041c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0444\u0430\u0439\u043b\u043e\u0432      *      * @param string $filePath \u043f\u0443\u0442\u044c \u0434\u043e \u0444\u0430\u0439\u043b\u0430      * @param string $dirPath \u043f\u0443\u0442\u044c \u0434\u043e \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 \u043d\u0430 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a      *      * @return string      *\/     public function disk_resources_upload(string $filePath, string $dirPath = ''): string     {         $arrParams = [             'path' => $dirPath . basename($filePath),             'overwrite' => 'true',         ];          $urlQuery = 'https:\/\/cloud-api.yandex.net\/v1\/disk\/resources\/upload';         $resultQuery = $this->sendQueryYaDisk('resources\/upload', $arrParams);          if (empty($resultQuery['error'])) {             $fp = fopen($filePath, 'r');                      $ch = curl_init($resultQuery['href']);             curl_setopt($ch, CURLOPT_PUT, true);             curl_setopt($ch, CURLOPT_UPLOAD, true);             curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filePath));             curl_setopt($ch, CURLOPT_INFILE, $fp);             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);             curl_setopt($ch, CURLOPT_HEADER, false);             $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);             curl_close($ch);                      return $http_code;         } else {             return $resultQuery['message'];         }     }                \/**      * \u041c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u043e\u0432 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430      *      * @param string $filePath \u043f\u0443\u0442\u044c \u0434\u043e \u0444\u0430\u0439\u043b\u0430 \u0432 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0435      * @param string $dirPath \u043f\u0443\u0442\u044c \u0434\u043e \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435      *      * @return array      *\/     public function disk_resources_download(string $filePath, string $dirPath = ''): array     {         $arrParams = [             'path' => $filePath,         ];         $resultQuery = $this->sendQueryYaDisk('resources\/download', $arrParams);          if(empty($resultQuery['error'])) {             $file_name = $dirPath . basename($filePath);             $file = @fopen($file_name, 'w');                      $ch = curl_init($resultQuery['href']);             curl_setopt($ch, CURLOPT_FILE, $file);             curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: OAuth ' . $this->token));             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);             curl_setopt($ch, CURLOPT_HEADER, false);             $resultQuery = curl_exec($ch);             curl_close($ch);              fclose($file);              return [                 'message' => '\u0424\u0430\u0439\u043b \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d',                 'path' => $file_name,             ];         } else {             return $resultQuery;         }     }       }<\/code><\/pre>\n<h3>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043e\u0431\u0449\u0435\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e\u0431 \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u0435 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0430 \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$resultQuery = $backupClass->sendQueryYaDisk();<\/code><\/pre>\n<h3>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043c\u0435\u0442\u0430\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e \u043f\u0430\u043f\u043a\u0430\u0445 \u0438 \u0444\u0430\u0439\u043b\u0430\u0445 \u043d\u0430 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0435 \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$arrParams = [     'path' => '\/uploads',     'fields' => 'name,_embedded.items.path',     'limit' => 100,     'offset' => 0,     'preview_crop' => false,     'preview_size' => '',     'sort' => 'created' ]; $resultQuery = $backupClass->sendQueryYaDisk('resources', $arrParams);<\/code><\/pre>\n<h3>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043c\u0435\u0442\u0430\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e \u043f\u0430\u043f\u043a\u0430\u0445 \u0438 \u0444\u0430\u0439\u043b\u0430\u0445 \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0435 \u043d\u0430 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0435 \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$arrParams = [     'path' => '\/',     'fields' => 'name,_embedded.items.path',     'limit' => 100,     'offset' => 0,     'preview_crop' => false,     'preview_size' => '',     'sort' => 'created' ]; $resultQuery = $backupClass->sendQueryYaDisk('trash\/resources', $arrParams);<\/code><\/pre>\n<h3>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043f\u043b\u043e\u0441\u043a\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0432\u0441\u0435\u0445 \u0444\u0430\u0439\u043b\u043e\u0432 \u0441 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0430 \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$arrParams = [     'limit' => 100,     'media_type' => 'image',     'offset' => 0,     'fields' => 'name,_embedded.items.path',     'preview_size' => '',     'preview_crop' => false, ]; $resultQuery = $backupClass->sendQueryYaDisk('resources\/files', $arrParams);<\/code><\/pre>\n<h3>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043d\u0430 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$arrParams = [     'limit' => 10,     'media_type' => 'image',     'fields' => 'name,_embedded.items.path',     'preview_size' => '',     'preview_crop' => false, ]; $resultQuery = $backupClass->sendQueryYaDisk('resources\/last-uploaded', $arrParams);<\/code><\/pre>\n<h3>\u041c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 \u043d\u0430 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0435 \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$arrParams = [     'path' => '\/uploads\/prog_time',     'fields' => 'name,_embedded.items.path', ]; $resultQuery = $backupClass->sendQueryYaDisk('resources', $arrParams, 'PUT');<\/code><\/pre>\n<h3>\u041c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0444\u0430\u0439\u043b\u043e\u0432 \u043d\u0430 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$filePath = $_SERVER['DOCUMENT_ROOT'] . '\/public\/image.png'; $dirPath = '\/uploads'; $resultQuery = $backupClass->disk_resources_upload($filePath, $dirPath);<\/code><\/pre>\n<h3>\u041c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u043e\u0432 \u0441 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0430 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$filePath = '\/test.docx'; $dirPath = $_SERVER['DOCUMENT_ROOT'] . '\/public'; $resultQuery = $backupClass->disk_resources_download($filePath, $dirPath);<\/code><\/pre>\n<h3>\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u0430 \u0441 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$arrParams = [     'path' => '\/test.docx',     'permanently' => false,     'fields' => 'name,_embedded.items.path', ]; $resultQuery = $backupClass->sendQueryYaDisk('resources', $arrParams, 'DELETE');<\/code><\/pre>\n<h3>\u041f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u044f \u0444\u0430\u0439\u043b\u0430 \u0432 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0435 \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$arrParams = [     'path' => '\/uploads\/test.xlsx', ]; $resultQuery = $backupClass->sendQueryYaDisk('resources\/publish', $arrParams, 'PUT');<\/code><\/pre>\n<h3>\u0421\u043d\u044f\u0442\u0438\u0435 \u0441 \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438 \u0444\u0430\u0439\u043b\u0430 \u0432 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0435 \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$arrParams = [     'path' => '\/uploads\/test.xlsx', ]; $resultQuery = $backupClass->sendQueryYaDisk('resources\/unpublish', $arrParams, 'PUT');<\/code><\/pre>\n<h3>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u044b\u0445 \u0444\u0430\u0439\u043b\u043e\u0432 \u0441 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0430 \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$arrParams = [     'limit' => 10,     'offset' => 0,     'type' => 'dir',     'fields' => 'name,_embedded.items.path',     'preview_size' => '', ]; $resultQuery = $backupClass->sendQueryYaDisk('resources\/public', $arrParams);<\/code><\/pre>\n<h3>\u041c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0432\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430 \u0438\u0437 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u0432 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0435 \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$arrParams = [     'path' => 'trash:\/test.docx_f8fb153e7cb73695ee2fdada79a7871b0093596e',     'name' => 'new_name.docx' ]; $resultQuery = $backupClass->sendQueryYaDisk('trash\/resources\/restore', $arrParams, 'PUT');<\/code><\/pre>\n<h3>\u041e\u0447\u0438\u0441\u0442\u043a\u0430 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u0432 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0435 \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$resultQuery = $backupClass->sendQueryYaDisk('trash\/resources', [], 'DELETE');<\/code><\/pre>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/749462\/\"> https:\/\/habr.com\/ru\/articles\/749462\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0417\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u044f \u043e\u0441\u043d\u043e\u0432\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u043a\u0443\u0440\u0441\u0430, \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u0443\u0440\u043e\u043a\u0435 \u043c\u044b \u0441 \u0432\u0430\u043c\u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432 API \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0430. \u041c\u044b \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u043c \u0432\u0435\u0441\u044c \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u0440\u0430\u043d\u0435\u0435 \u043a\u043e\u0434 \u0438 \u043e\u0441\u0442\u0430\u0432\u0438\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b.<\/p>\n<p>\u0417\u043d\u0430\u044f \u043c\u0435\u0442\u043e\u0434\u044b \u0438 URL \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u044f \u0440\u0435\u0448\u0438\u043b \u0443\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043e\u0431\u043e\u043b\u043e\u0447\u043a\u0430\u043c\u0438 \u0438 \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043a \u043c\u0435\u0442\u043e\u0434\u0443 <strong>sendQueryYaDisk()<\/strong>.<\/p>\n<p>\u0422\u0430\u043a \u0436\u0435 \u044f \u0440\u0435\u0448\u0438\u043b \u0441\u0442\u0440\u043e\u043a\u0443 <strong>https:\/\/cloud-api.yandex.net\/v1\/disk<\/strong> \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e <strong>$basicApiUrl<\/strong>, \u0430 \u043c\u0435\u0442\u043e\u0434 <strong>sendQueryYaDisk()<\/strong> \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u0435\u0440\u0432\u044b\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u043d\u0435 \u043f\u043e\u043b\u043d\u044b\u0439 URL, \u0430 \u0435\u0433\u043e \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u0435 \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434\u044b.<\/p>\n<h3>\u041a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code> class Backup {     protected $token = '\u0442\u043e\u043a\u0435\u043d';     protected $basicApiUrl = 'https:\/\/cloud-api.yandex.net\/v1\/disk\/';      \/**      * Method sendQueryYaDisk      *      * @param string $urlQuery URL \u0434\u043b\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432      * @param array $arrQuery \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432      * @param string $methodQuery \u043c\u0435\u0442\u043e\u0434 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438      *      * @return array      *\/     public function sendQueryYaDisk(string $methodAPI = '', array $arrQuery = [], string $methodQuery = 'GET'): array     {         if($methodQuery == 'POST') {             $fullUrlQuery = $this->basicApiUrl . $methodAPI;         } else {             $fullUrlQuery = $this->basicApiUrl . $methodAPI . '?' . http_build_query($arrQuery);         }          $ch = curl_init($fullUrlQuery);         switch ($methodQuery) {             case 'PUT':                 curl_setopt($ch, CURLOPT_PUT, true);                 break;              case 'POST':                 curl_setopt($ch, CURLOPT_POST, 1);             curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($arrQuery));                 break;              case 'DELETE':                 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');                 break;         }          curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: OAuth ' . $this->token]);         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);         curl_setopt($ch, CURLOPT_HEADER, false);         $resultQuery = curl_exec($ch);         curl_close($ch);          return (!empty($resultQuery)) ? json_decode($resultQuery, true) : [];     }            \/**      * \u041c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0444\u0430\u0439\u043b\u043e\u0432      *      * @param string $filePath \u043f\u0443\u0442\u044c \u0434\u043e \u0444\u0430\u0439\u043b\u0430      * @param string $dirPath \u043f\u0443\u0442\u044c \u0434\u043e \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 \u043d\u0430 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a      *      * @return string      *\/     public function disk_resources_upload(string $filePath, string $dirPath = ''): string     {         $arrParams = [             'path' => $dirPath . basename($filePath),             'overwrite' => 'true',         ];          $urlQuery = 'https:\/\/cloud-api.yandex.net\/v1\/disk\/resources\/upload';         $resultQuery = $this->sendQueryYaDisk('resources\/upload', $arrParams);          if (empty($resultQuery['error'])) {             $fp = fopen($filePath, 'r');                      $ch = curl_init($resultQuery['href']);             curl_setopt($ch, CURLOPT_PUT, true);             curl_setopt($ch, CURLOPT_UPLOAD, true);             curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filePath));             curl_setopt($ch, CURLOPT_INFILE, $fp);             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);             curl_setopt($ch, CURLOPT_HEADER, false);             $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);             curl_close($ch);                      return $http_code;         } else {             return $resultQuery['message'];         }     }                \/**      * \u041c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u043e\u0432 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430      *      * @param string $filePath \u043f\u0443\u0442\u044c \u0434\u043e \u0444\u0430\u0439\u043b\u0430 \u0432 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0435      * @param string $dirPath \u043f\u0443\u0442\u044c \u0434\u043e \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435      *      * @return array      *\/     public function disk_resources_download(string $filePath, string $dirPath = ''): array     {         $arrParams = [             'path' => $filePath,         ];         $resultQuery = $this->sendQueryYaDisk('resources\/download', $arrParams);          if(empty($resultQuery['error'])) {             $file_name = $dirPath . basename($filePath);             $file = @fopen($file_name, 'w');                      $ch = curl_init($resultQuery['href']);             curl_setopt($ch, CURLOPT_FILE, $file);             curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: OAuth ' . $this->token));             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);             curl_setopt($ch, CURLOPT_HEADER, false);             $resultQuery = curl_exec($ch);             curl_close($ch);              fclose($file);              return [                 'message' => '\u0424\u0430\u0439\u043b \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d',                 'path' => $file_name,             ];         } else {             return $resultQuery;         }     }       }<\/code><\/pre>\n<h3>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043e\u0431\u0449\u0435\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e\u0431 \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u0435 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0430 \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$resultQuery = $backupClass->sendQueryYaDisk();<\/code><\/pre>\n<h3>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043c\u0435\u0442\u0430\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e \u043f\u0430\u043f\u043a\u0430\u0445 \u0438 \u0444\u0430\u0439\u043b\u0430\u0445 \u043d\u0430 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0435 \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$arrParams = [     'path' => '\/uploads',     'fields' => 'name,_embedded.items.path',     'limit' => 100,     'offset' => 0,     'preview_crop' => false,     'preview_size' => '',     'sort' => 'created' ]; $resultQuery = $backupClass->sendQueryYaDisk('resources', $arrParams);<\/code><\/pre>\n<h3>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043c\u0435\u0442\u0430\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e \u043f\u0430\u043f\u043a\u0430\u0445 \u0438 \u0444\u0430\u0439\u043b\u0430\u0445 \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0435 \u043d\u0430 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0435 \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$arrParams = [     'path' => '\/',     'fields' => 'name,_embedded.items.path',     'limit' => 100,     'offset' => 0,     'preview_crop' => false,     'preview_size' => '',     'sort' => 'created' ]; $resultQuery = $backupClass->sendQueryYaDisk('trash\/resources', $arrParams);<\/code><\/pre>\n<h3>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043f\u043b\u043e\u0441\u043a\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0432\u0441\u0435\u0445 \u0444\u0430\u0439\u043b\u043e\u0432 \u0441 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0430 \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$arrParams = [     'limit' => 100,     'media_type' => 'image',     'offset' => 0,     'fields' => 'name,_embedded.items.path',     'preview_size' => '',     'preview_crop' => false, ]; $resultQuery = $backupClass->sendQueryYaDisk('resources\/files', $arrParams);<\/code><\/pre>\n<h3>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043d\u0430 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$arrParams = [     'limit' => 10,     'media_type' => 'image',     'fields' => 'name,_embedded.items.path',     'preview_size' => '',     'preview_crop' => false, ]; $resultQuery = $backupClass->sendQueryYaDisk('resources\/last-uploaded', $arrParams);<\/code><\/pre>\n<h3>\u041c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 \u043d\u0430 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0435 \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$arrParams = [     'path' => '\/uploads\/prog_time',     'fields' => 'name,_embedded.items.path', ]; $resultQuery = $backupClass->sendQueryYaDisk('resources', $arrParams, 'PUT');<\/code><\/pre>\n<h3>\u041c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0444\u0430\u0439\u043b\u043e\u0432 \u043d\u0430 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$filePath = $_SERVER['DOCUMENT_ROOT'] . '\/public\/image.png'; $dirPath = '\/uploads'; $resultQuery = $backupClass->disk_resources_upload($filePath, $dirPath);<\/code><\/pre>\n<h3>\u041c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u043e\u0432 \u0441 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0430 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$filePath = '\/test.docx'; $dirPath = $_SERVER['DOCUMENT_ROOT'] . '\/public'; $resultQuery = $backupClass->disk_resources_download($filePath, $dirPath);<\/code><\/pre>\n<h3>\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u0430 \u0441 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$arrParams = [     'path' => '\/test.docx',     'permanently' => false,     'fields' => 'name,_embedded.items.path', ]; $resultQuery = $backupClass->sendQueryYaDisk('resources', $arrParams, 'DELETE');<\/code><\/pre>\n<h3>\u041f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u044f \u0444\u0430\u0439\u043b\u0430 \u0432 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0435 \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$arrParams = [     'path' => '\/uploads\/test.xlsx', ]; $resultQuery = $backupClass->sendQueryYaDisk('resources\/publish', $arrParams, 'PUT');<\/code><\/pre>\n<h3>\u0421\u043d\u044f\u0442\u0438\u0435 \u0441 \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438 \u0444\u0430\u0439\u043b\u0430 \u0432 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0435 \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$arrParams = [     'path' => '\/uploads\/test.xlsx', ]; $resultQuery = $backupClass->sendQueryYaDisk('resources\/unpublish', $arrParams, 'PUT');<\/code><\/pre>\n<h3>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u044b\u0445 \u0444\u0430\u0439\u043b\u043e\u0432 \u0441 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0430 \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$arrParams = [     'limit' => 10,     'offset' => 0,     'type' => 'dir',     'fields' => 'name,_embedded.items.path',     'preview_size' => '', ]; $resultQuery = $backupClass->sendQueryYaDisk('resources\/public', $arrParams);<\/code><\/pre>\n<h3>\u041c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0432\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430 \u0438\u0437 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u0432 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0435 \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$arrParams = [     'path' => 'trash:\/test.docx_f8fb153e7cb73695ee2fdada79a7871b0093596e',     'name' => 'new_name.docx' ]; $resultQuery = $backupClass->sendQueryYaDisk('trash\/resources\/restore', $arrParams, 'PUT');<\/code><\/pre>\n<h3>\u041e\u0447\u0438\u0441\u0442\u043a\u0430 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u0432 \u042f\u043d\u0434\u0435\u043a\u0441.\u0414\u0438\u0441\u043a\u0435 \u0447\u0435\u0440\u0435\u0437 API<\/h3>\n<pre><code>$resultQuery = $backupClass->sendQueryYaDisk('trash\/resources', [], 'DELETE');<\/code><\/pre>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/749462\/\"> https:\/\/habr.com\/ru\/articles\/749462\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-350559","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/350559","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=350559"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/350559\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=350559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=350559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=350559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}