簡體   English   中英

如何調試錯誤“不建議使用:curl_setopt():”

[英]How to debug the error “Deprecated: curl_setopt():”

我有一個舊腳本,該腳本通過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");

進行上述操作已消除了該錯誤,但是實際上無法打開上傳文件的潛在問題仍然在發生。 所以我想知道我做錯了什么嗎?

從PHP 5.5及更高版本開始,您應該使用CURLFile上傳文件,我已經發布了描述CURLFile和正常文件上傳的完整答案,您可以在此處檢查該答案。

您可以按以下方式使用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是您用來上傳文件的鏈接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM