繁体   English   中英

使用 cURL 和发票生成器 API 检索 PDF 文件

[英]Retrieving PDF file with cURL and an invoice generator API

对于我本学期的最后一个大学项目,我尝试使用在网络上找到的 API 生成发票,并使用 cURL 调用它,我认为这是最简单的选项。

不幸的是,当我执行代码时,我没有收到可读的页面或 pdf 文件,我不知道如何从这里开始。

任何帮助将不胜感激!

      <?php



    $invoice = array(
        "logo" => "test.dev", // url to logo
        "from" => "Wouter van Marrum \n<a href=\"http://www.concept-core.nl\">concept-core.nl</a>", // The name of your organization
        "to" => "marleen", // The entity being billed - multiple lines ok
        "number" => 20001, // Invoice number
        //"purchase_order" => "", // Purchase order number
        "date" => null, // Invoice date
        "due_date" => null, // Invoice due date
        "payment_terms" => null, // Payment terms summary (i.e. NET 30)
        // items
        "items[0][name]" => "Website",
        "items[0][quantity]" => 1,
        "items[0][unit_cost]" => 300,
        "items[1][name]" => "logo",
        "items[1][quantity]" => 1,
        "items[1][unit_cost]" => 150,
        // End items
        // Fields
        "fields[tax]" => true, // can be true, false
        "fields[discounts]" => false, // Subtotal discounts - numbers only
        "fields[shipping]" => false, // Can be true or false
        // End fields
        "tax"=> 21, // Tax - numbers only
        "amount_paid" => 0, // Amount paid - numbers only
        "notes" => "", // Notes - any extra information not included elsewhere
        "terms" => "Lees dit", // Terms and conditions - all the details
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://invoice-generator.com");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $invoice);


    $result = curl_exec($ch);

  $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://invoice-generator.com");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $invoice);


    $result = curl_exec($ch);

    header('Content-Description: File Transfer');
    header('Content-type: application/pdf');
    header('Content-Disposition: attachment; filename="invoice_'.$date->toDateString().'.pdf"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: '.strlen($result));




    curl_close($ch);



    echo $result;

我在这里找到了这个 API 的文档( https://github.com/wotta/Invoiced-backup/blob/master/app/ConceptCore/Invoiced/Core.php )但我不明白我错过了哪些额外的步骤.

只是给你一个想法,这是我在执行代码时看到的一部分:

%PDF-1.4 1 0 obj << /Title ( Invoice) /Creator ( wkhtmltopdf 0.12.2.1) /Producer ( Qt 4.8.6) /CreationDate (D:20191206214540Z) >> endobj 3 0 obj < < /Type /ExtGState /SA true /SM 0.02 /ca 1.0 /CA 1.0 /AIS false /SMask /None>> endobj 4 0 obj [/Pattern /DeviceRGB] endobj 8 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 5 0 obj << /Type /Page /Parent 2 0 R /Contents 9 0 R /Resources 11 0 R /Annots 12 0 R /MediaBox [0 0 612 792] >> endobj 11 0 obj << /色彩空间 << /PCSp 4 0 R /CSp /DeviceRGB /CSpg /DeviceGray >> /ExtGState << /GSa 3 0 R >> /Pattern << >> /Font << /F6 6 0 R /F7 7 0 R > > /XObject << >> >> endobj 12 0 obj [ ] endobj 9 0 obj << /Length 10 0 R /Filter /FlateDecode >> stream x \\Ko 6 W @ @Q Σ@ a C mZ, E = "i Ɏ7 Rn Ҍ y}C): ?= N J. & y 1 K ? ( \\Q n :/ y |鸉 Gx`+&u u L t p K s L> >> > ;o AW vS r A l K . ( 26 BX w ^ Kf PD2 ϝ6R W 7] h)Ӂ^ S҅u Kk ]h;a2P j*ױ?E ]Ig g K ˱ B '; ň U ?tO!^ (P @ }75< U 9+LE ʊ .E uW Glj R R )a:m & U J | b ӳ~ ] "-y|"Ʉ d P <~"? H?w L x G> 4: Ҝ[{ ; ks̹ _ yìS2 Kwh 7 K rq A GA t^s C l + Jkj a,nz ~ {) )I) R/ ~# u] 3 7 fi NWt -%V A wA T R i O ZN ԰q B eRY[+{ 4 Oqx B YeM c @ \\eF^ P 43m q M^ L ѐc ye0e Dd9 L[ ]L WA< =& AE ׵ F a o \\ 9 NڛJ}1 '-GUp 2x) 1S 6 dѦό3 7} ; h o ? eK K W 3 ;ć^C4ZU Ait ee zM;Q )

您需要添加curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 指定 CURL 将结果作为字符串返回而不是格式化。 在这种情况下,使用此代码,结果将是一个可读的 PDF 文件:结果 pdf 的屏幕截图

此外,在这种情况下,您必须替换header('Content-Length: ' . $response->getHeader('Content-Length')[0]); to header('Content-Length: '.strlen($result)); 并且由于CURLOPT_RETURNTRANSFER标志,您将不会使用$file = $result-> getBody(); ,尝试删除它。

我附上了一个带有修改后代码的链接,目前在我的服务器上工作: https : //pastebin.com/JDgKjUt8

我建议您启用错误日志以验证出了什么问题: 显示所有错误和警告

暂无
暂无

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

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