[英]Download file directly from URL to local folder or a folder in remote server
我已經從鏈接下載了一個文件,並且必須保存在我的本地系統文件夾或遠程服務器文件夾中。 場景是:我有一個 mailgun 域,當我向它發送郵件時,Mailgun 存儲函數( store()
)將它與所有附件一起存儲並通知我。 來自 mailgun 的響應在catch_email_attachment()
中catch_email_attachment()
,我能夠獲取響應並獲得附加文件的鏈接。 當我直接在瀏覽器中運行鏈接時,它會給我附加文件,沒問題。 但是我需要在catch_email_attachment()
下載文件並將其保存在一個文件夾中。
可下載的文件是: “ https://API:<API-KEY>@api.mailgun.net/v2/domains/sandboxa6e6ebce3f68475aa3xxxxxxxd60.mailgun.org/messages/eyJwIjogZmFsc2UsICJrIjogImQ0MmZjxxxxxxxxxxxxxxDQwNy1iYzhlLTA2OWMxY2U3MDg2NCIsIxxxxxxxxxxxxxx1Y2UiLCAiYyI6ICJpYWR0cmFpbGVycyJ9/attachments/0
”
我的代碼如下:
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/');
}
}
}
我參考了: https : //stackoverflow.com/a/26330976/4229270
https://stackoverflow.com/a/6594030/4229270
https://stackoverflow.com/a/724449/4229270
你能幫我解決這個問題嗎……提前致謝。
看起來$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));
}
}
上面的回答對我很有幫助。 我所做的和得到的是:
Mailgun 將響應我們在 maingun 域中設置的鈎子函數。 捕獲來自 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
}
}
您必須從 Mailgun 響應中檢查端點。 並且,必須設置各自的 in 或 code。 此外,我們必須設置API KEY & PATH TO DOWNLOAD 。
Mailgun 的存儲鏈接將通過“api.mailgun.net”指向“xx.api.mailgun.net”。 它將像“si.api.mailgun.net”、“so.api.mailgun.net”、“se.api.mailgun.net”、“sw.api.mailgun.net”等。URL 到獲取附件數據將是
API返回的數據會返回獲取存儲消息的URL:(items>attachments>url)
請參考: https : //documentation.mailgun.com/en/latest/api-sending.html#retrieving-stored-messages
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.