简体   繁体   English

直接从 URL 下载文件到本地文件夹或远程服务器中的文件夹

[英]Download file directly from URL to local folder or a folder in remote server

I have download a file from the link & has to save in my local system folder or in a remote server folder.我已经从链接下载了一个文件,并且必须保存在我的本地系统文件夹或远程服务器文件夹中。 The scenario is that: I have a mailgun domain, when I send a mail to it, Mailgun store function ( store() ) stores it with all attachments & notifies me.场景是:我有一个 mailgun 域,当我向它发送邮件时,Mailgun 存储函数( store() )将它与所有附件一起存储并通知我。 The response from mailgun is catched in catch_email_attachment() , I'm able fetch the response & got the link of attached files.来自 mailgun 的响应在catch_email_attachment()catch_email_attachment() ,我能够获取响应并获得附加文件的链接。 When I run the link directly in browser it gives me the attached file, no problem on that.当我直接在浏览器中运行链接时,它会给我附加文件,没问题。 But I need to download the file inside catch_email_attachment() & to save it in a folder.但是我需要在catch_email_attachment()下载文件并将其保存在一个文件夹中。

The downloadable file is as: " https://API:<API-KEY>@api.mailgun.net/v2/domains/sandboxa6e6ebce3f68475aa3xxxxxxxd60.mailgun.org/messages/eyJwIjogZmFsc2UsICJrIjogImQ0MmZjxxxxxxxxxxxxxxDQwNy1iYzhlLTA2OWMxY2U3MDg2NCIsIxxxxxxxxxxxxxx1Y2UiLCAiYyI6ICJpYWR0cmFpbGVycyJ9/attachments/0 "可下载的文件是: “ https://API:<API-KEY>@api.mailgun.net/v2/domains/sandboxa6e6ebce3f68475aa3xxxxxxxd60.mailgun.org/messages/eyJwIjogZmFsc2UsICJrIjogImQ0MmZjxxxxxxxxxxxxxxDQwNy1iYzhlLTA2OWMxY2U3MDg2NCIsIxxxxxxxxxxxxxx1Y2UiLCAiYyI6ICJpYWR0cmFpbGVycyJ9/attachments/0

My codes are below:我的代码如下:

public function catch_email_attachment()
{
    $data = $this->input->post(null, true);
    if (!empty($data)) {
        if (isset($data['attachments'])) {
            /*
            Output of $data['attachments'] is below:
            [{"url": "https://api.mailgun.net/v2/domains/sandboxa6e6ebce3f68475aa3xxxxxxxd60.mailgun.org/messages/eyJwIjogZmFsc2UsICJrIjogImQ0MmZjxxxxxxxxxxxxxxDQwNy1iYzhlLTA2OWMxY2U3MDg2NCIsIxxxxxxxxxxxxxx1Y2UiLCAiYyI6ICJpYWR0cmFpbGVycyJ9/attachments/0", "content-type": "image/jpeg", "name": "xxxxxxx.jpeg", "size": 9498}]
            */

            copy('https://API:key-e5ae9afab1fa9xxxxxxxxxxxxxxa95a@api.mailgun.net/v2/domains/sandboxa6e6ebce3f68475axxxxxxxxxxxxxxxxxxxxxxxxxxxx.mailgun.org/messages/eyJwIjogZmFsc2UxxxxxxxxxxxxxxxxxxxxxxxxxxxxmUtNDQwNy1iYzhlLTA2OWMxY2U3MDg2NCIxxxxxxxxxxxxxxxxxxxxxxxxxxxx1Y2UiLCAiYyI6ICJpYWR0cmFpbGVycyJ9/attachments/0', '/var/www/download_loc/');
        }
    }
}

I have refered: https://stackoverflow.com/a/26330976/4229270我参考了: https : //stackoverflow.com/a/26330976/4229270

https://stackoverflow.com/a/6594030/4229270 https://stackoverflow.com/a/6594030/4229270

https://stackoverflow.com/a/724449/4229270 https://stackoverflow.com/a/724449/4229270

Can you help me to solve the issue... Thanking in advance.你能帮我解决这个问题吗……提前致谢。

It looks like $data['attachments'] is a json array so you need something like:看起来$data['attachments']是一个 json 数组,所以你需要这样的东西:

    $attachments = json_decode($data['attachments']);
        $api_key = 'APIKEY';
        if ($attachments) {
            foreach ($attachments as $attachment) {

                $context = stream_context_create(array(
                    'http' => array(
                        'header'  => "Authorization: Basic " . base64_encode("API:$api_key")
                    )
                ));

                file_put_contents('/var/www/download_loc/' . $attachment->name, file_get_contents($attachment->url, false, $context));

            }
        }

The above answer is surly helpful for me.上面的回答对我很有帮助。 What I have done & got is:我所做的和得到的是:

Mailgun will give response to our hook function which we set in our maingun domain. Mailgun 将响应我们在 maingun 域中设置的钩子函数。 Catch the response from Mailgun as:捕获来自 Mailgun 的响应:

$data = $this->input->post(null, true);
$attachments = json_decode($data['attachments']);
$apikey = 'API:<API_KEY>@';
foreach ($attachments as $attachment) {
    $st = strpos($attachment->url, 'https://');
    if ($st !== false) {
        $attachment->url = str_replace('https://', 'https://'.$apikey, $attachment->url);
        $temp_name = $attachment->name;
        $file_path = '<PATH_TO_DOWNLOAD>'.$temp_name;
        copy($attachment->url, $file_path); // Downloading file to our path
    }
}

You have to check endpoint from Mailgun response.您必须从 Mailgun 响应中检查端点。 And, have to set respective in or code.并且,必须设置各自的 in 或 code。 Also, we have to set API KEY & PATH TO DOWNLOAD .此外,我们必须设置API KEY & PATH TO DOWNLOAD

The storage link from Mailgun will be to "xx.api.mailgun.net" over "api.mailgun.net". Mailgun 的存储链接将通过“api.mailgun.net”指向“xx.api.mailgun.net”。 It will be like "si.api.mailgun.net", "so.api.mailgun.net", "se.api.mailgun.net", "sw.api.mailgun.net", etc.. The URL to obtain the attachment data would be它将像“si.api.mailgun.net”、“so.api.mailgun.net”、“se.api.mailgun.net”、“sw.api.mailgun.net”等。URL 到获取附件数据将是

Eg: https://API:key-60mvxxxxxxxxxxxxxxxx@sw.api.mailgun.net/v3/domains/YOUR_MAILGUN_DOMAIN/messages/xxxxxxxxxxxxxxxxxxxxxxxxxxx=/attachments/0例如: https://API:key-60mvxxxxxxxxxxxxxxxx@sw.api.mailgun.net/v3/domains/YOUR_MAILGUN_DOMAIN/messages/xxxxxxxxxxxxxxxxxxxxxxxxxxx=/attachments/0

The data returned from the API will return the URL to obtain the stored message : (items > attachments > url) API返回的数据会返回获取存储消息的URL:(items>attachments>url)

Please refer: https://documentation.mailgun.com/en/latest/api-sending.html#retrieving-stored-messages请参考: https : //documentation.mailgun.com/en/latest/api-sending.html#retrieving-stored-messages

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM