我试图从使用php的Gmail电子邮件正文中提到的自动下载链接下载zip文件

时间:2017-03-27 05:41:33

标签: php csv email curl download

我每天都会从供应商处收到有关gmail的电子邮件,其中包含自动下载CSV文件的zip文件夹的链接。我试图通过编写脚本来访问电子邮件服务器,搜索未读电子邮件,然后访问电子邮件正文中的链接,然后将zip文件夹保存在本地目录(如下载文件夹)中,然后自动化数据库上载部分将CSV文件导入数据库,最终将其作为cron作业运行。我曾尝试编写一个执行上述操作的脚本(除了将数据从CSV文件导入数据库的最后一部分),但无法访问电子邮件中的链接以开始自动下载。我试图使用DOM文档,curl,header和其他一些东西,但是不能让脚本做我想做的同样的事情。我在下面粘贴我的代码。我无法访问电子邮件正文中的链接并强制从该链接下载zip文件夹到本地文件夹。请帮助我实现上述目标

<?php

/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'example_email_address@gmail.com' ;
$password = 'password';

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

/* grab emails */
$emails = imap_search($inbox,'ALL');

/* if emails are returned, cycle through each... */
if($emails) {

    /* begin output var */
    $output = '';

    /* put the newest emails on top */
    rsort($emails);

    /* for every email... */
    foreach($emails as $email_number) {

        /* get information specific to this email */
        $overview = imap_fetch_overview($inbox,$email_number,0);
        $message = imap_fetchbody($inbox,$email_number,2);

        // $regex = '/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i';
        // preg_match_all($regex, $message, $matches);
        // $urls = $matches[0];
        // // go over all links
        // foreach($urls as $url) 
        //     {
        //         echo $url.'<br />';
        //     }

        // $dom = new DOMDocument;
        // //$dom->loadXML($message);
        // $dom->loadHTML($message);
        // $anchortags = $dom->getElementsByTagName('a');
        // foreach ($anchortags as $anchortag) {
        //     echo $anchortag->nodeValue, PHP_EOL;
        //     preg_match('//([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?', $anchortag, $matches);
        //     }



        // /* output the email header information */
        // $output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
        // $output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
        // $output.= '<span class="from">'.$overview[0]->from.'</span>';
        // $output.= '<span class="date">on '.$overview[0]->date.'</span>';
        // $output.= '</div>';

        // /* output the email body */
        // $output.= '<div class="body">'.$message.'</div>';
    }

    echo $output;
} 

function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

$returned_content = get_data('https://dash-jci.s3.amazonaws.com/February_11_2017/Feb_11_2017-1486798132143.zip?response-cache-control=max-age%3D31536000&response-content-type=application%2Fzip&AWSAccessKeyId=AKIAJREL4U5PR4NTGPKA&Expires=2135333220&Signature=8FxLAICc89HQMc98LQH6dsIYyp4%3Dfff');



/* close the connection */
imap_close($inbox);

?>

0 个答案:

没有答案