Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过直接链接将文件下载到服务器_Php_Yandex_Yandex Api - Fatal编程技术网

Php 通过直接链接将文件下载到服务器

Php 通过直接链接将文件下载到服务器,php,yandex,yandex-api,Php,Yandex,Yandex Api,任务-有一个下载文件的直接链接,您需要使用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.yand

任务-有一个下载文件的直接链接,您需要使用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.";
}

?>

您可以使用以下功能:

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