K
K
Kirill Gorelov2020-04-11 20:49:56
PHP
Kirill Gorelov, 2020-04-11 20:49:56

Curl download file?

Hello.

What is the situation, you need to download the file from i.disk.
I took the ready-made nixsolutions/yandex-php-library library, it works.
I change the token to another, with exactly the same settings, authorization fails. Super support for Yandex, just unsubscribed, saying "we do not guarantee the ability of our services";)

Well ....
I start writing my class, it works, BUT the method of downloading the picture does not work.

spoiler

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

        // echo $res;
            $res = json_decode($res);
            $file_name = $localPath;
            var_dump($file_name);
            var_dump($localPath);
            var_dump($res->href);
            $file = fopen($file_name, 'w');
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $res->href);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: OAuth ' . $this->token));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_HEADER, false);
            curl_setopt($ch, CURLOPT_FILE, $file);
            $res = curl_exec($ch);
            curl_close($ch);
            fclose($file);
            var_dump($res);



The file downloads but is empty. It weighs zero bytes ...
There is a similar question , but there the guy solved the problem using wget, and this option does not roll.
All you need is to understand how to download the file and that's it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AlexRsk, 2020-04-11
@Kirill-Gorelov

Try using file_get_contents instead of cuRL

$context  = stream_context_create([
                "ssl" => [
                    "verify_peer"      => false,
                    "verify_peer_name" => false,
                ],
            ]);
            header('Content-Type: application/octet-stream');
            header('Content-Transfer-Encoding: Binary');      
            header('Content-Disposition: attachment; filename="' . $fileName . '"');
            echo file_get_contents($link, false, $context);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question