U
U
upxbot2017-02-11 14:16:09
PHP
upxbot, 2017-02-11 14:16:09

Yandex.Disk, uploading files via webdav. What is the problem?

Greetings. To upload files (and not only uploads) to Yandex.disk, there is the following class:

<?php

class YandexDisk {

    public $ch;
    public $ansver, $info;
    public $headers = array();

    function __construct($user, $pass) {
        $this->headers[] = "Authorization: Basic " . base64_encode($user . ":" . $pass);

        $this->ch = curl_init();
        curl_setopt($this->ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17");

        curl_setopt($this->ch, CURLOPT_HEADER, 0);
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($this->ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false);
    }

//-----------------//
    function my_exec() {
        curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->headers);
        $this->ansver = curl_exec($this->ch);
        $this->info = curl_getinfo($this->ch);
        print_r($this->info);
        curl_close($this->ch);
    }

//--------получить файл---------//
    function get($url) {
        curl_setopt($this->ch, CURLOPT_URL, 'https://webdav.yandex.ru' . $url);
        $this->my_exec();
        if ($this->info['http_code'] != '200')
            return FALSE; // Error!
        return $this->ansver;
    }

//--------удалить файл---------//
    function delete($url) {
        curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
        curl_setopt($this->ch, CURLOPT_URL, 'https://webdav.yandex.ru' . $url);
        $this->my_exec();
        if ($this->info['http_code'] != '204')
            return FALSE; // Error!
        return TRUE;
    }

//-------закачать файл----------//
    function put($file, $url) {
        curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PUT');
        curl_setopt($this->ch, CURLOPT_URL, 'https://webdav.yandex.ru' . $url);
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, file_get_contents($file));

        $this->my_exec();
        if ($this->info['http_code'] != '201')
            return FALSE; // Error!

        return TRUE;
    }

//-------Список файлов в директории----------//
    function ls($dir = '') {
        $this->headers[] = 'Depth: 1';
        curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PROPFIND');
        curl_setopt($this->ch, CURLOPT_URL, 'https://webdav.yandex.ru' . $dir);
        $this->my_exec();

        if ($this->info['http_code'] != '207')
            return FALSE; // Error!


        $xml = simplexml_load_string($this->ansver);
        $xml->registerXPathNamespace('d', 'urn:DAV');
        $res = array();

        foreach ($xml->xpath('/d:multistatus/d:response/d:href') as $v) {
            $res[] = urldecode($v);
        }

        return $res;
    }

//-----------------//
    function mkdir($dir) {
        curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'MKCOL');
        curl_setopt($this->ch, CURLOPT_URL, 'https://webdav.yandex.ru' . $dir);
        $this->my_exec();
        if ($this->info['http_code'] != '201')
            return FALSE; // Error!
        return TRUE;
    }

//-----------------//
}

The application is simple:
$disk = new YandexDisk('логин', 'пароль');
$disk->put('файл', 'файл');

So, if I upload a file weighing 0 bytes, then it is loaded, and if more, then it is not. What could be the problem?
Response from the server on unsuccessful download:
Array ( [url] => https://webdav.yandex.ru/lokalka.11022017-0801.zip [content_type] => [http_code] => 500 [header_size] => 220 [request_size] => 378 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.664637 [namelookup_time] => 0.012795 [connect_time] => 0.058902 [pretransfer_time] => 0.307447 [size_upload] => 933888 [size_download] => 0 [speed_download] => 0 [speed_upload] => 1405109 [download_content_length] => 0 [upload_content_length] => 20144632 [starttransfer_time] => 0.433229 [redirect_time] => 0 [redirect_url] => [primary_ip] => 87.250.250.53 [certinfo] => Array ( ) [primary_port] => 443 [local_ip] =>  [local_port] => 60710 )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2017-02-11
@dimonchik2013

from the protocol (Yandex webdav is not pure vybdav) to versioned language / curl

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question