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

使用PHP从远程服务器下载PDF,php,pdf,fopen,file-get-contents,Php,Pdf,Fopen,File Get Contents,我正在尝试使用PHP下载一个.PDF文件。我曾多次使用此脚本下载JPEG,但由于某些原因,PDF给我带来了麻烦 我可以一个接一个地手动下载这些PDF,但这需要很长时间 $csv = array_map('str_getcsv', file('upc.csv')); $count = count($csv) - 1; $i = '0'; while($i <= 2){ $data_upc = $csv[$i][0]; $curl = 'si

我正在尝试使用PHP下载一个.PDF文件。我曾多次使用此脚本下载JPEG,但由于某些原因,PDF给我带来了麻烦

我可以一个接一个地手动下载这些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++;

    }
$csv=array\u map('str\u getcsv',file('upc.csv');
$count=计数($csv)-1;
$i='0';

当($i使用此函数时,在其上循环并输入PDF url

  • $url
    是完整的PDF url
  • $copyPath
    是新文件的完整路径
职能:
function downloadFile($url, $copyPath) {
    $pdf = file_get_contents($url);
    file_put_contents ($copyPath, $pdf);
}
编辑 例如,如何在函数上循环:

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);
}

使用这个函数,在其上循环并输入PDF url

  • $url
    是完整的PDF url
  • $copyPath
    是新文件的完整路径
职能:
function downloadFile($url, $copyPath) {
    $pdf = file_get_contents($url);
    file_put_contents ($copyPath, $pdf);
}
编辑 例如,如何在函数上循环:

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);
}

“这需要很长时间”你是什么意思?如果我手动一个接一个地下载它们,会有什么问题。这需要很长时间。你没有传递这些信息,但我想象你有一个html链接列表,你想下载,对吗?是的,我正在抓取我想要的页面并建立到pdf的链接。“这需要很长时间”你是什么意思?如果我手动一个接一个地下载它们,会有什么问题。这会花很长时间。你没有传递这些信息,但我想象你有一个html链接列表,你想下载,对吗?是的,我正在抓取我想要的页面并建立指向pdf的链接。这不起作用。我收到一个错误,说该功能无法运行重新声明。我猜你循环了它。检查我的编辑我猜他有两个相同的函数更像是将函数减速放在循环中没有,如果有pdf没有打开,这不起作用。我收到一个错误,说函数不能重新声明。我猜你循环了它。检查我的编辑我猜他有两个相同的fuNCtions更像是将函数减速放在循环中,没有,如果有,pdf不会打开