0

I have an old script that uploads PDF files via an API. I'm getting the error message:

Deprecated: curl_setopt(): The usage of the @filename API for file uploading is deprecated. Please use the CURLFile class instead.

Here is the relevant code (I think this is all of it). The error points to the line: 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;

    }

My knowledge is pretty basic. But I've tried to replace:

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

with

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

Doing the above has got rid of the error, but the underlying problem where I can't actually open the uploaded file is still happening. So I'm wondering if I've done something wrong?

Metzed
  • 470
  • 1
  • 8
  • 27
  • 1
    Correct you use http://php.net/manual/en/class.curlfile.php or http://php.net/manual/en/function.curl-file-create.php instead of the `@` notation – Scuzzy Aug 14 '18 at 08:25
  • 2
    Explicitly answering the question "Is this the way to solve “Deprecated: curl_setopt():” error?" - Yes. Use something else. It's deprecated. – ggdx Aug 14 '18 at 08:26

1 Answers1

1

From PHP 5.5 and above you should use CURLFile to upload file, I have already posted a complete answer describing CURLFile and normal file upload, you can check that answer here.

You can use CURLFile as below, feel free to adjust the code as per your need:

//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);
}

As your file and filetype are in an array named $postFields, so you can call the above function as below:

upload($target, $postFields);

where $target is the link which you are calling to upload the file.

Tejashwi Kalp Taru
  • 2,994
  • 2
  • 20
  • 35
  • Thank you for your help Tejashwi - I've just edited the code in the original question to show the complete function to hopefully give a more complete picture. Because in my function the file is in an array, do I need to alter your line here: $file = "@" . SERVERPATH . "quotes/quote-". $this->id .".pdf"; or any other line? – Metzed Aug 14 '18 at 16:03