在mpdf->输出之后可以使用curl吗?

问题描述

这是我的代码

$mpdf->WriteHTML($html);
$fname = date('Y-m-d-h-s') . "-" . $randomnumber . ".pdf";
$mpdf->Output(__DIR__ . "/../../backend/web/uploads/" . $fname);

$filePath = __DIR__ . "/../../backend/web/uploads/" . $fname;
$filePath = curl_file_create($filePath);
$fileOB = file_get_contents($filePath);
$data = array(
    'token' => $phone,'id' => $staff_id,'file' => $filePath,);
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl,CURLOPT_HEADER,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl,CURLOPT_HTTPHEADER,array('Content-type: text/xml'));

$response = curl_exec($curl);
curl_close($curl);
echo $response;

但是我没有得到回应。因此,可以在mdpf->output()之后使用curl吗?

更新:我的PHP版本低于5.5

解决方法

我当前的php版本没有curl_file_create(),所以我写了一个条件,要求在缺席时使用替代技术。

if (function_exists('curl_file_create')) { // php 5.5+
    $filePath = curl_file_create($filePath);
} else {
    $filePath = '@' . realpath($filePath);
}

代码现在可以正常工作。