3 февраля 2013 г.

Яндекс Диск и PHP

Возникла необходимость сохранения данных на Яндкс Диске. Погуглив нашёл класс для работы с ЯндексДиском с помощью базовой авторизации без использования Oauth - https://github.com/KhArtNJava/phpYandexDisk




Написал обёртку для данного класса:
<?

require_once('class_webdav_client.php');

class yandexDiskException extends Exception { }

class yandexDisk {
 /**
     * @var webdav_client
     */
 var $wdc;
 var $errorMsg = '';
 
 public function __construct($login, $password){
  $this->wdc = new webdav_client();
  $this->wdc->set_server( 'ssl://webdav.yandex.ru' );
  $this->wdc->set_port( 443 );
  $this->wdc->set_user( $login );
  $this->wdc->set_pass( $password );
  // use HTTP/1.1
  $this->wdc->set_protocol( 1 );
  // enable debugging
  $this->wdc->set_debug( true );
  if ( !$this->wdc->open() ) {
      throw new yandexDiskException("Error: could not open server connection");
  }
  // check if server supports webdav rfc 2518
  if ( !$this->wdc->check_webdav() ) {
      throw new yandexDiskException('Error: server does not support webdav or user/password may be wrong');
  }
 }
 
 public function __destruct(){
  $this->wdc->close();
  flush();
 }
 
 public function createFolder($folderName){
  if (empty($folderName)) {
   $this->errorMsg = 'Folder Name could not be empty';
   return false;
  }
  $http_status = $this->wdc->mkcol( '/'.$folderName.'/' );
  switch ($http_status){
   case 201:
    return true;
    break;
   default:
    $this->errorMsg = 'Error ocured '.$http_status;
    return false;
    break;
  }
 }
 
 public function uploadFile($source_file, $destination_folder, $destination_filename = ''){
  if (empty($source_file)) {
   $this->errorMsg = 'File could not be empty';
   return false;
  }
  if (empty($destination_folder)) {
   $this->errorMsg = 'Destination could not be empty';
   return false;
  }
  if (empty($destination_filename)) {
   $pathinfo = pathinfo($source_file);
   $destination_filename = $pathinfo['basename'];
  }
  
  $http_status = $this->wdc->put_file( '/'.$destination_folder.'/'.$destination_filename, $source_file );
  switch ($http_status){
   case 200:
   case 201:
   case 204:
    return true;
    break;
   default:
    $this->errorMsg = 'Error ocured '.$http_status;
    return false;
    break;
  }
 }
 
 public function downloadFile($serverFilePath, $localFilePath){
  if (empty($serverFilePath) || empty($localFilePath)) {
   $this->errorMsg = 'File Path could not be empty';
   return false;
  }
  $res = $this->wdc->get_file( $serverFilePath, $localFilePath );
  return $res;
 }
 
 public function publishFile($filePath){
  if (empty($filePath)) {
   $this->errorMsg = 'File Path could not be empty';
   return false;
  }
  $res = $this->wdc->filePublish( $filePath );
  return $res;
 }
 
 public function unPublishFile($filePath){
  if (empty($filePath)) {
   $this->errorMsg = 'File Path could not be empty';
   return false;
  }
  $res = $this->wdc->fileUnPublish( $filePath );
  return $res;
 }
 
}


?>
Пример использования:
<?


include_once('yandexDisk.php');


try {
 $yandexDisk = new yandexDisk('login', 'password');
 $res = $yandexDisk->createFolder('test');
 $res = $yandexDisk->uploadFile('test.png', 'test');
 $res = $yandexDisk->publishFile('/test/test.png');
 $res = $yandexDisk->downloadFile('/test/test.png', 'test222.png');
}catch (yandexDiskException $e){
 echo $e->getMessage();
}



Комментариев нет:

Отправить комментарий