Automatically upload files to Yandex.Disk

    I bring to your attention a PHP script to automatically upload files to the Yandex.Disk service. The script is simple to disgrace, just pass it the login, password and path to the file as parameters, and as a result, it will upload your file to the service and give you a link to it. I don’t know how much more Yandex will endure with my posts, but still I’ll give the source code of the script :)

    Copy Source | Copy HTML
    1. print uploadFile(ваш_логин, ваш_пароль, путь_к_файлу)."\n";
    2.  
    3. function uploadFile($login, $password, $filename)
    4. {
    5.     $cookie_file = 'cookie.txt';
    6.     $user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6';
    7.  
    8.     // логинимся в систему
    9.     $ch = curl_init('https://passport.yandex.ru/passport?mode=auth');
    10.  
    11.     $fields = array();
    12.     $fields[] = "login=$login";
    13.     $fields[] = "passwd=$password";
    14.     $fields[] = "twoweeks=yes";
    15.     curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $fields));
    16.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    17.     curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
    18.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    19.     curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
    20.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,  0);
    21.     curl_setopt($ch, CURLOPT_POST, 1);
    22.     $result = curl_exec($ch);
    23.     $info = curl_getinfo($ch);
    24.  
    25.     if ($info['http_code'] != 200) return false;
    26.  
    27.     // запрашиваем сервер для загрузки файла
    28.     $url = 'http://narod.yandex.ru/disk/getstorage/?rnd=' . (mt_rand( 0, 777777) + 777777);
    29.  
    30.     curl_setopt($ch, CURLOPT_URL, $url);
    31.     curl_setopt($ch, CURLOPT_POST,  0);
    32.     curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
    33.     $result = curl_exec($ch);
    34.     $info = curl_getinfo($ch);
    35.  
    36.     if (preg_match('/"url":"(.*?)", "hash":"(.*?)", "purl":"(.*?)"/', $result, $m)) {
    37.       $upload_url = $m[1];
    38.       $hash = $m[2];
    39.       $purl = $m[3];
    40.     } else {
    41.         return false;
    42.     }
    43.  
    44.     // загружаем файл на сервер
    45.     $url = $upload_url . '?tid=' . $hash;
    46.     $fields = array();
    47.     $fields['file'] = '@' . $filename;
    48.     curl_setopt($ch, CURLOPT_URL, $url);
    49.     curl_setopt($ch, CURLOPT_REFERER, 'http://narod.yandex.ru/');
    50.     curl_setopt($ch, CURLOPT_POST, 1);
    51.     curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    52.     $result = curl_exec($ch);
    53.     $info = curl_getinfo($ch);
    54.  
    55.     if ($info['http_code'] != 200) return false;
    56.  
    57.  //   print_r($info);
    58.     // проверяем прогресс бар
    59.     $url = $purl . '?tid=' . $hash . '&rnd=' . (mt_rand( 0, 777777) + 777777);
    60.  
    61.     curl_setopt($ch, CURLOPT_URL, $url);
    62.     curl_setopt($ch, CURLOPT_POST,  0);
    63.     $result = curl_exec($ch);
    64.  
    65.     if (!preg_match('/"status": "done"/', $result, $m)) {
    66.       return false;
    67.     }
    68.  
    69.     // переходим на страницу и определяем ссылку
    70.     $url = 'http://narod.yandex.ru/disk/last/';
    71.     curl_setopt($ch, CURLOPT_URL, $url);
    72.     $result = curl_exec($ch);
    73.     curl_close($ch);
    74.  
    75.     if (preg_match('//', $result, $m)) {
    76.       $fileURL = trim($m[1]);
    77.       return $fileURL;
    78.     }
    79.  
    80.     return false;
    81. }
    82. ?>
    83.  

    Also popular now: