Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP文件\u获取\u动态生成URL的内容/pdf_Php_Html_Url_Pdf_Zip - Fatal编程技术网

PHP文件\u获取\u动态生成URL的内容/pdf

PHP文件\u获取\u动态生成URL的内容/pdf,php,html,url,pdf,zip,Php,Html,Url,Pdf,Zip,我想在URL数组中循环并创建一个zip文件。URL是动态生成的pdf文件/发票。该程序可以很好地处理物理上存在的文件,但对于动态生成的文件,它获取的是html源代码/页面,而不是pdf 除了使用:file\u gets\u contents之外,还有其他方法吗 下面是一个模拟代码: // prepare array of URL's/files $files = array( "1" => "http://localhost/app/addon/test.pdf", //wor

我想在URL数组中循环并创建一个zip文件。URL是动态生成的pdf文件/发票。该程序可以很好地处理物理上存在的文件,但对于动态生成的文件,它获取的是html源代码/页面,而不是pdf

除了使用:file\u gets\u contents之外,还有其他方法吗

下面是一个模拟代码:

// prepare array of URL's/files

$files = array(
     "1" => "http://localhost/app/addon/test.pdf", //works
     "2" => "http://localhost/app/addon/test2.pdf",//works
     "3" => "http://localhost/app/index.php?section=flight-fatturazione&option=getPdf&tipo=invoice&id=6", //doesn't work
     "4" => "http://localhost/app/index.php?section=flight-fatturazione&option=getPdf&tipo=invoice&id=4",
     "5"=>  "http://localhost/app/index.php?section=flight-fatturazione&option=getPdf&tipo=invoice&id=2"
);

// create new zip opbject
$zip = new ZipArchive();

// create a temp file & open it
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);

// loop through url/file array
foreach($files as $file){
   // download file
   // this gets file contents, what to do when file is created on the fly
   $download_file = file_get_contents($file);
   // add it to the zip
   $zip->addFromString(basename($file),$download_file);
}
   // close zip
$zip->close();

   // send the file to the browser as a download
header('Content-disposition: attachment; filename=download.zip');
header('Content-type: application/zip');
readfile($tmp_file);

目前,代码创建了一个包含2个pdf文件(即文件:test.pdf、test2.pdf)和3个html文件的zip,即使访问URL直接生成pdf

您正在将文件3-5保存为.php文件。将它们重命名为.pdf有效吗


如您所说,如果您正在接收html文件,它们是否包含生成它们的PHP代码?(比如,它是否返回index.php的代码?)。如果这两种方法都不起作用,您是否需要特定的cookie或其他设置(会话?)才能请求获取pdf?

经过长时间的尝试,我解决了这个问题。正如我在问题中提到的,我正在动态生成发票,并试图使用

file_get_contents
通过传递生成的发票的url。这将返回zip中的html页面,而不是pdf。有人建议使用curl并模拟http请求,但这并没有解决问题。所有这些都是为了避免应用程序中的代码重复,但最终我别无选择

因此,对于每个发票,我调用动态创建它的函数,然后使用file\u get\u contents获取内容

function createInvoiceContent($invoice_id){

   require_once('tcpdf_include.php');

   // create new PDF document
   $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

   /* Do a bunch of stuff to create the invoice */



   // Close and output PDF document
   // This method has several options, check the source code documentation  for more information.
   $invoice_content = $pdf->Output('invoice_name.pdf', 'I');
   return $invoice_content;
}
我可以在需要时调用这个函数

   //get pdf invoice  file
    $invoice = file_get_contents(createInvoiceContent($invoice_id));

URL,3-5当被访问时,打开pdf/发票文件(当用户登录时)。我的想法是获取pdf并将其添加到zip,然后下载。调试时,$download\u file=file\u get\u contents($file);包含html页面而不是预期的pdf。“当用户登录时”。如果用户没有登录,他们会得到一个HTML页面?这可以解释为什么file_get_contents()返回HTML页面。它请求的页面没有任何cookie或会话集(=未登录)。您可以使用curl首次登录,获取cookies,然后使用curl下载页面,或者使用简单的方法,检查$\u SERVER['REMOTE\u ADDR'],如果请求是从localhost发出的,然后允许它。或者,在index.php中的另一个文件中创建PDF,您可以使用该文件在运行时创建该文件。