【问题标题】:Download the file to the server by direct link通过直接链接将文件下载到服务器
【发布时间】:2019-11-12 11:24:31
【问题描述】:

任务-有一个直接下载文件的链接,需要用php的方法下载这个文件并保存在服务器上。 有代码,但是不加载文件,只是新建一个。

<?php
$token = 'token';
// file from yandex disk.
$yd_file = '/FermerPrice/test.jpeg/';

// save this.
$path = download;

//get array with link
$ch = curl_init('https://cloud-api.yandex.net/v1/disk/resources/download?path=' . urlencode($yd_file));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: OAuth ' . $token));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$res = curl_exec($ch);
curl_close($ch);

// $res['href'] - link download
$res = json_decode($res, true);


if (empty($res['error'])) {
    $file_name = $path . '/' . basename($yd_file);
    $file = @fopen($file_name, 'w');

    $ch = curl_init($res['href']);
    curl_setopt($ch, CURLOPT_FILE, $file);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: OAuth ' . $token));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_exec($ch);
    curl_close($ch);
    fclose($file);
    echo "Good.";
}

?>

【问题讨论】:

标签: php yandex yandex-api


【解决方案1】:

你可以用这个:

file_put_contents("localfilename", file_get_contents('https://cloud-api.yandex.net/v1/disk/resources/download?path=' . urlencode($yd_file)));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-26
    • 2017-09-11
    • 2020-12-18
    • 1970-01-01
    • 2021-11-16
    • 2015-07-12
    • 2017-08-11
    相关资源
    最近更新 更多