【发布时间】:2016-04-11 06:31:45
【问题描述】:
您好,我已经成功地从我的表单提交中生成了一个 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--
【问题讨论】:
-
您在发送文件之前是否对其进行了测试?你试过比较两边的文件大小吗?
-
您好。是的,我的本地目录中的 pdf 文件很好。文件大小不同。因此,它不是 CRM 中的实际文件,而是 CRM 从 curl_url 生成的 tmp 文件。我可以使用手动文件上传输入字段发布文件,但不能在上面的代码中自动发布我想要的文件。我尝试创建一个新的 CURLFILE 来将其分解为 $cfile = new CURLFILE($_FILES[$pdf]['tmp_name']...) 但我知道 $_FILES 仅用于文件上传。谢谢
-
好吧..缩小了一点,但问题仍然可能存在于任何一方。您也应该发布接收代码.. 很有可能他会卷曲.. 您是否将其发布到已经从另一个脚本接收的端点?
-
CURLOPT_POST 需要一个布尔值,应该更改为 1 或 true,但不要认为这是问题
-
另外,你需要删除json_encoding,应该是一个数组而不是一个json字符串..