我从链接&中下载了一个文件必须保存在我的本地系统文件夹或远程服务器文件夹中。方案是:我有一个mailgun域,当我向其发送邮件时,Mailgun商店功能(store()
)存储它与所有附件&通知我来自mailgun的响应被catch_email_attachment()
捕获,我可以获取响应&得到了附件的链接。当我直接在浏览器中运行链接时,它会给我附加文件,没问题。但我需要在catch_email_attachment()
&内部下载文件。将其保存在文件夹中。
可下载文件如下:&#34; https://API:<API-KEY>@api.mailgun.net/v2/domains/sandboxa6e6ebce3f68475aa3xxxxxxxd60.mailgun.org/messages/eyJwIjogZmFsc2UsICJrIjogImQ0MmZjxxxxxxxxxxxxxxDQwNy1iYzhlLTA2OWMxY2U3MDg2NCIsIxxxxxxxxxxxxxx1Y2UiLCAiYyI6ICJpYWR0cmFpbGVycyJ9/attachments/0
&#34;
我的代码如下:
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
你能帮我解决这个问题吗...提前感谢。
答案 0 :(得分:1)
看起来$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));
}
}
答案 1 :(得分:0)
上述答案对我很有帮助。我做了什么&amp;得到的是:
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响应中检查端点。并且,必须分别设置或代码。另外,我们必须设置 API KEY &amp; 下载路径。
Mailgun的存储链接将通过“api.mailgun.net”发送到“xx.api.mailgun.net”。它将像“si.api.mailgun.net”,“so.api.mailgun.net”,“se.api.mailgun.net”,“sw.api.mailgun.net”等。获取附件数据
从API返回的数据将返回URL以获取存储的消息:( items&gt; attachments&gt; url)
请参阅:https://documentation.mailgun.com/en/latest/api-sending.html#retrieving-stored-messages