繁体   English   中英

使用PHP从远程服务器下载PDF

[英]Download PDF from remote server using PHP

我正在尝试使用PHP下载.PDF。 我已经多次使用此脚本来下载jpeg,但是由于某种原因却给我带来了麻烦。

我可以一张一张地手动下载这些PDF,但是要花一些时间。

    $csv = array_map('str_getcsv', file('upc.csv'));
    $count = count($csv) - 1;

    $i = '0';
    while($i <= 2){
    $data_upc = $csv[$i][0];

    $curl = 'site.com'.$data_upc.'#colors';
    $ch = curl_init($curl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36');
    curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    // No certificate
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);     // Follow redirects
    curl_setopt($ch, CURLOPT_MAXREDIRS, 4);             // Limit redirections to four
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    $response = curl_exec($ch);
    curl_close($ch);

    preg_match('/<a href="\/assets\/hlr-system(.*?)</',$response,$match);

    $pdf_url = $match[1];

    $pdf_url = 'beginning-url'.str_replace('" class="pdf">Spec Sheet','',$pdf_url);

    $pdf = 'brand/'.$data_upc.'.pdf';
    file_put_contents($pdf, fopen($pdf_url));


    echo 'testing: '.$pdf_url;
    echo '<br />';

    $i++;

    }

使用此功能,对其进行循环并输入PDF网址。

  • $url是完整的PDF URL
  • $copyPath是新文件的完整路径

功能:

function downloadFile($url, $copyPath) {
    $pdf = file_get_contents($url);
    file_put_contents ($copyPath, $pdf);
}

编辑

例如如何遍历函数:

$arr[] = 'http://link1';
$arr[] = 'http://link2';
$arr[] = 'http://link3';

$num = 1;
foreach ($arr as $link) {
    downloadFile($url, "file{$num}.pdf");
    $num++;
}

function downloadFile($url, $copyPath) {
    $pdf = file_get_contents($url);
    file_put_contents ($copyPath, $pdf);
}

暂无
暂无

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

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