FPDF file ---> send via CURL automatically NOT with file upload

前端 未结 1 1367
孤独总比滥情好
孤独总比滥情好 2021-01-26 08:45

Hi I have managed to successfully generate a FPDF pdf file from my form submission. I have then POSTED it via CURL to an API endpoint. This \'file\' shows inside the CRM system

相关标签:
1条回答
  • 2021-01-26 09:34

    Hi I managed to get this working. I am not sure if this is the best way of doing it but it worked.

    It was to convert the output of the fpdf as a string $doc = $pdf->Output($filename,'S'); then decode it before sending the curl var_dump(json_decode($doc, true)); & also make the header content type "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);
    

    If anyone is interested this is my solution for using file upload instead of the fpdf send solution. Multipart form file upload POST PHP cURL

    0 讨论(0)
提交回复
热议问题