FPDF文件--->通过CURL自动发送不与文件上传

时间:2016-01-07 00:38:30

标签: php api pdf curl fpdf

您好我已成功从表单提交中生成FPDF pdf文件。然后我通过CURL将它发布到API端点。 这个'文件'显示在CRM系统里面,但我无法打开它。 我认为这根本不是文件,并且没有正确传递存储/抓取。

由于

见下面的代码:

$noteTitle = (isset($_POST['noteTitle']) ? $_POST['noteTitle'] : null);
$noteBody = (isset($_POST['noteBody']) ? $_POST['noteBody'] : null);

require("fpdf/fpdf.php");
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont("Arial", "B", 16);
$pdf->Cell(0,100, "Note Title: {$noteTitle} \n", 1, 0, 'C');
$pdf->Cell(0,100, "Note Body: {$noteBody}", 1, 0, 'C');
$filename="testFPDF.pdf";
$pdf->Output($filename,'F');  

$headers2 = array(
    "authorization: Basic xxx",
    "content-type: multipart/form-data",
    "cache-control: no-cache",
    "postman-token: xxx"
);   

$file_name_with_full_path = 'C:/localhost/insightly-php/testFPDF.pdf';
$timeout = 30;
$target_url = "https://api.insight.ly/v2.1/Notes/?c_id=" . $noteID . "&filename=" . $filename;
$parameters =  array(
    'FILE_ATTACHMENTS' => '@' . $file_name_with_full_path,
    'CONTENT_TYPE' => 'application/pdf'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers2);
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");    
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
//curl_setopt($ch, CURLOPT_ENCODING,"");
//curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
//curl_setopt($ch, CURLOPT_MAXREDIRS,10);
//curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
//curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
//curl_setopt( $ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_exec($ch);
curl_close ($ch);

文件内容为:

--------------------------xxxxxxxxxxxxxxxx
Content-Disposition: form-data; name="FILE_ATTACHMENTS"

@C:/localhost/insightly-php/testFPDF.pdf
--------------------------xxxxxxxxxxxxxxxx--

1 个答案:

答案 0 :(得分:0)

嗨,我设法让这个工作。我不确定这是否是最好的方法,但它有效。

将fpdf的输出转换为字符串$ doc = $ pdf->输出($ filename,' S');然后在发送curl var_dump(json_decode($ doc,true))之前对其进行解码; &安培;还制作标题内容类型" application / pdf"

$noteTitle = (isset($_POST['noteTitle']) ? $_POST['noteTitle'] : null);
$noteBody = (isset($_POST['noteBody']) ? $_POST['noteBody'] : null);

//FPDF
require("fpdf/fpdf.php");
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont("Arial", "B", 14);
$pdf->Cell(0,50, "Note Title: {$noteTitle}", 1, 0, 'C');
$pdf->Cell(0,50, "Note Body: {$noteBody}", 1, 0, 'C');
$filename="testFPDF.pdf";
$doc = $pdf->Output($filename,'S');     

///////////FIRST POST REQUEST////////////
$orgID = 70239678;
$addNote = (object)array(        
        "TITLE" => "$noteTitle",
        "BODY" => "$noteBody ",
        "LINK_SUBJECT_ID" => "$orgID",
        "LINK_SUBJECT_TYPE" => "Organisation"              
);   
$addNote = $i->addNote($addNote); 

///////////GET NOTE ID////////////
$orgNotes = $i->getNotes(array("orderby" => 'DATE_CREATED_UTC'));
foreach ($orgNotes as $note) {        
      $noteID = $note->NOTE_ID;          
}

///////////SEND ATTACHEMENT////////////         
$headers2 = array(
    "authorization: Basic xxx",
    "content-type: application/pdf",
    "cache-control: no-cache",
    "postman-token: xxx"
);       

$target_url = "https://api.insight.ly/v2.1/Notes/?c_id=" . $noteID . "&filename=" . $filename;

var_dump(json_decode($doc, true));
$parameters = array(
    'FILE_ATTACHMENT' => $doc  
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers2);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_exec($ch);
curl_close ($ch);

如果有人有兴趣,这是我使用文件上传而不是fpdf发送解决方案的解决方案。 Multipart form file upload POST PHP cURL