S
S
Salavat2014-02-19 08:50:44
PHP
Salavat, 2014-02-19 08:50:44

How to programmatically download a file from Yandex.Disk?

Good afternoon.
There are files on the Yandex disk with public links, for example, https://disk.yandex.ru/public/?hash=Zn9rQik51OUzcW...
How to programmatically download a file, for example, in php?

Answer the question

In order to leave comments, you need to log in

7 answer(s)
R
Rufat Nuriev, 2015-09-11
@nrr

Here is an example implementation in php:

function get_stat( $url, $headers )
{
    $handle = curl_init();
    curl_setopt( $handle, CURLOPT_URL, $url );
    curl_setopt( $handle, CURLOPT_HTTPHEADER, $headers );
    curl_setopt( $handle, CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt( $handle, CURLOPT_SSL_VERIFYHOST, false );
    curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true );
    $response = curl_exec( $handle );
    $code = curl_getinfo( $handle, CURLINFO_HTTP_CODE );
    return array( "code" => $code, "response" => $response );
}

$url_yandex_disk = "https://yadi.sk/i/byMUqoSLiw3Ki";

$result = get_stat( "https://cloud-api.yandex.net:443/v1/disk/public/resources/download?public_key=" . urlencode( $url_yandex_disk ), array() );
if( $result["code"] == 200 )
{
  $result["response"] = json_decode( $result["response"], true );
  echo '<a href="' . $result["response"]["href"] . '">Скачать</a>';
}
else
{
  echo "error";
}

and here is a working example:
https://getfile.dokpub.com/yandex/
And here is publishing and getting a list of your files on Yandex disk:
https://getfile.dokpub.com/yandex/publish/
For those who want to generate constantly updated links passed, for example in a GET parameter, can be done like this:
$url_yandex_disk = $_GET["ya_url"];

$result = get_stat( "https://cloud-api.yandex.net:443/v1/disk/public/resources/download?public_key=" . urlencode( $url_yandex_disk ), array() );
if( $result["code"] == 200 )
{
  $result["response"] = json_decode( $result["response"], true );
  header( "Location: " . $result["response"]["href"], true, 302 );
  exit( 0 );
}

and here in Python:
import requests
import pprint
import urllib.parse

targetUrl = "https://yadi.sk/i/03bE933n3PqpG2"
url = "https://cloud-api.yandex.net/v1/disk/public/resources?public_key=" + urllib.parse.quote(targetUrl, safe="")

responseData = requests.get(url)
if responseData.headers["content-type"] in ["application/json; charset=utf-8", "application/json"]:
    print(pprint.pformat(responseData.json()))
else:
    print(responseData)

In some cases, errors may occur.
Below are related questions with the current answer:
How to correctly use a direct link from Yandex.disk?

D
DeFacto, 2014-02-19
@DeFacto

https://github.com/KhArtNJava/phpYandexDisk
see this link

A
AxisPod, 2014-02-19
@AxisPod

Request with HEAD method? It can be the same curl.

F
fart, 2014-02-19
@fart

webdav + php

X
xandox, 2014-02-19
@xandox

if the links are public, download with any http client

N
nikolaman, 2015-05-18
@nikolaman

did you solve the problem?
I have a public link to the file, I need to somehow download it.
https://yadi.sk/d/Rzdip...
I can't find where to start
according to https://tech.yandex.ru/disk/api/reference/public-d... I need a publick_key, but where can I get it?

M
mc-star, 2016-02-01
@mc-star

Good day to all, but tell me how to make it so that it would be like on the site https://getfile.dokpub.com/yandex/
When after https://getfile.dokpub.com/yandex/get/Yandex link
Gives a direct link to the file .
Just do it on your site, for example, so that I enter http://my.say/get/Yandex link
And everything worked OK!
Tell me please(

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question