Php “如何调试错误”;不推荐使用:curl_setopt():";
我有一个通过API上传PDF文件的旧脚本。我收到了错误消息: 不推荐使用:curl_setopt():文件的@filename API的用法 不推荐上载。请改用CURLFile类 这是相关代码(我想这就是全部)。错误指向以下行:Php “如何调试错误”;不推荐使用:curl_setopt():";,php,Php,我有一个通过API上传PDF文件的旧脚本。我收到了错误消息: 不推荐使用:curl_setopt():文件的@filename API的用法 不推荐上载。请改用CURLFile类 这是相关代码(我想这就是全部)。错误指向以下行:curl\u setopt($curl\u handle,CURLOPT\u POSTFIELDS,$POSTFIELDS) 我的知识相当基础。但我试图替换: $postFields['file'] = "@" . SERVERPATH . "quotes/quote-"
curl\u setopt($curl\u handle,CURLOPT\u POSTFIELDS,$POSTFIELDS)
我的知识相当基础。但我试图替换:
$postFields['file'] = "@" . SERVERPATH . "quotes/quote-". $this->id .".pdf";
$postFields['type'] = "application/pdf";
与
执行上述操作已经消除了错误,但无法实际打开上载文件的潜在问题仍然存在。所以我想知道我是否做错了什么?在PHP5.5及以上版本中,您应该使用CURLFile来上传文件,我已经发布了一个完整的答案,描述了CURLFile和普通文件上传,您可以检查这个答案 您可以按如下方式使用CULLFILE,根据需要随时调整代码:
//Upload file using CURLFile
function upload($target, $postFields){
$file = $postFields['file'];
$cFile = new CURLFile($file,$postFields['type'], $file);
$data = array(
'file' => $cFile,
'type' => $postFields['type'],
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $target);
curl_setopt($curl, CURLOPT_HEADER , true); //we need header
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true); // enable posting
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // post images
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // if any redirection after upload
curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
$r = curl_exec($curl);
if (curl_errno($curl)) {
$error = curl_error($curl);
print_r($error);
} else {
// check the HTTP status code of the request
$resultStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($resultStatus != 200) {
print_r($resultStatus);
}else{
//successfull
print_r($r);
}
}
curl_close($curl);
}
由于文件和文件类型位于名为$postFields
的数组中,因此可以按如下方式调用上述函数:
upload($target,$postFields)代码>
其中,$target
是您调用以上载文件的链接。请更正您使用的链接,或代替@
注释明确回答问题“这是解决问题的方法吗”弃用:curl_setopt():“错误?”-是。用别的东西。谢谢你的帮助Tejashwi-我刚刚编辑了原始问题中的代码,以显示完整的函数,希望能给出更完整的图片。因为在我的函数中,文件位于数组中,我是否需要在此处更改您的行:$file=“@”。服务器路径。“引用/引用-”$此->id。“.pdf”;还是其他线路?
$postFields['file'] = curl_file_create(SERVERPATH . "quotes/quote-". $this->id .".pdf", 'application/pdf', SERVERPATH . "quotes/quote-". $this->id .".pdf");
//Upload file using CURLFile
function upload($target, $postFields){
$file = $postFields['file'];
$cFile = new CURLFile($file,$postFields['type'], $file);
$data = array(
'file' => $cFile,
'type' => $postFields['type'],
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $target);
curl_setopt($curl, CURLOPT_HEADER , true); //we need header
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true); // enable posting
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // post images
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // if any redirection after upload
curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
$r = curl_exec($curl);
if (curl_errno($curl)) {
$error = curl_error($curl);
print_r($error);
} else {
// check the HTTP status code of the request
$resultStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($resultStatus != 200) {
print_r($resultStatus);
}else{
//successfull
print_r($r);
}
}
curl_close($curl);
}