如何调试错误“不建议使用:curl_setopt():”

时间:2018-08-14 08:24:35

标签: php

我有一个旧脚本,该脚本通过API上传PDF文件。我收到错误消息:

  

不建议使用:curl_setopt():文件的@filename API的用法    不建议上传。请改用CURLFile类。

这是相关的代码(我想这就是全部)。错误指向以下行:curl_setopt( $curl_handle, CURLOPT_POSTFIELDS, $postFields )

//Upload the file
    function uploadPdf( $api, $lead_id, $rev, $existing_files = array() ) {

        if ( ! file_exists( SERVERPATH . "quotes/quote-". $this->id .".pdf" ) )
            $this->createQuotePdf();

        $files_array = array( array( 'entityType'=>'files', 'name'=>"quote-". $this->id .".pdf" ) );

        // if ( $this->upfile && ! file_exists( SERVERPATH . "uploads/" . $upfile ) )
        //  $files_array[] = array( array( 'entityType'=>'files', 'name'=> $upfile ) );

        foreach ( $existing_files as $file ) {
            $files_array[] = (array) $file;
        }

        //this request gives us the URLs to upload to
        $result = $api->editLead( array( 'leadId' => $lead_id, 'rev'=>'REV_IGNORE', 'lead'=> array( 'file' => $files_array ) ) );

        //Upload the Quote file
        $postFields = array();      

        $postFields['file'] = "@" . SERVERPATH . "quotes/quote-". $this->id .".pdf";

        $postFields['type'] = "application/pdf";
        $curl_handle = curl_init();
        $file = array_pop( $result->file );
        curl_setopt( $curl_handle, CURLOPT_URL, $file->uri );
        curl_setopt( $curl_handle, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $curl_handle, CURLOPT_POST, true );
        curl_setopt( $curl_handle, CURLOPT_USERPWD, USERNAME . ":" . API_KEY );
        curl_setopt( $curl_handle, CURLOPT_POSTFIELDS, $postFields );
        //execute the API Call
        $return  = curl_exec( $curl_handle ) ;

        $this->uploadUpfile($api, $lead_id);

        return $return;

    }

我的知识很基础。但是我试图替换:

$postFields['file'] = "@" . SERVERPATH . "quotes/quote-". $this->id .".pdf";
$postFields['type'] = "application/pdf";

使用

$postFields['file'] = curl_file_create(SERVERPATH . "quotes/quote-". $this->id .".pdf", 'application/pdf', SERVERPATH . "quotes/quote-". $this->id .".pdf");

执行上述操作已消除了该错误,但是实际上无法打开上传文件的潜在问题仍在发生。所以我想知道我做错了什么吗?

1 个答案:

答案 0 :(得分:1)

从PHP 5.5及更高版本开始,您应该使用CURLFile上传文件,我已经发布了描述CURLFile和正常文件上传的完整答案,您可以检查答案here

您可以按以下方式使用CURLFile,可以根据需要随意调整代码:

//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是您用来上传文件的链接。