0

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 where supposed go but I cannot open it. I believe this is not the file at all and it is not being passed stored/grabbed properly.

Thanks

See below code:

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

File contents are:

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

@C:/localhost/insightly-php/testFPDF.pdf
--------------------------xxxxxxxxxxxxxxxx--
12
  • did you test the file before sending it? did you try comparing the filesize on both sides? Commented Jan 7, 2016 at 3:53
  • Hi there. Yes the pdf file is fine in my local directory. File sizes are different. So it is not the actual file sitting in the CRM but rather a tmp file the CRM is generating from the curl_url. I can post the file with a manual file upload input field but not automatically how I would like it in my above code. I tried creating a new CURLFILE to break it up like this $cfile = new CURLFILE($_FILES[$pdf]['tmp_name']...) but I understand $_FILES is only for file upload. Thanks Commented Jan 7, 2016 at 4:06
  • well.. that narrows it down a little, but the issue could still be on either side. you should post the receiving code as well.. good chance it is he curl tho.. are you posting this to an endpoint that is already recieving from another script? Commented Jan 7, 2016 at 4:19
  • CURLOPT_POST takes a bool, should change to 1 or true, but don't think that's the issue Commented Jan 7, 2016 at 4:20
  • also, you need to remove the json_encoding, should be an array not a json string.. Commented Jan 7, 2016 at 4:22

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.