2014-10-09 12 views
5

Ho trovato alcune domande relative a quanto sopra, ma non ho avuto alcuna risposta appropriata. Ecco il mio codice:curl_setopt() lanciare un'eccezione di errore durante il tentativo di caricare un file da curl in php

$requestUrl = <MY_URL>; 
$filePath = <MY_FILE_PATH>; 
$post = array('extra_info' => '123456','file_contents'=>'@'.$filePath); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$requestUrl); 
curl_setopt($ch, CURLOPT_POST,1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
$response = curl_exec($ch); 
curl_close($ch); 

echo $response; 

sto ottenendo un errore di eccezione come:

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

Qualsiasi aiuto sarà apprezzato!

+1

quale versione di PHP che si sta utilizzando? – Naincy

+1

Sto usando la versione 5.5.9 –

+0

Si prega di controllare la soluzione qui: http://stackoverflow.com/questions/20972513/php-curl-the-usage-of-the-filename-api-for-file- upload-è-deprecato – versha

risposta

6

partire da PHP 5.5.0, il Il prefisso @ è deprecato e i file possono essere inviati utilizzando CURLFile. È possibile controllare il manuale here

Prova questa:

$ch = curl_init($requestUrl); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLOPT_HTTPHEADER, 
array('Authorization: Bearer XXXXXXXXXXXXXXXYYYYYzzzzzz', 'Accept-Version: ~1', 'Except: 100-continue', 'Content-type: multipart/form-data')); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLINFO_HEADER_OUT, true); 

$args['file'] = new CurlFile('<file_path>', 'text/plain', 'myfile'); 

curl_setopt($ch, CURLOPT_POSTFIELDS, $args); 

$response = curl_exec($ch); 
4

prega di fare riferimento questo, può questo aiuto si http://pdfcrowd.com/forums/read.php?4,1930

PHP 5.5 ha introdotto un oggetto CurlFile che depreca la vecchia sintassi @nomefile Vedere anche: https://wiki.php.net/rfc/curl-file-upload

public function getCurlValue() 
{ 

    if (function_exists('curl_file_create')) { 
     return curl_file_create($this->filename, $this->contentType, $this->postname); 
    } 

    // Use the old style if using an older version of PHP 
    $value = "@{$this->filename};filename=" . $this->postname; 
    if ($this->contentType) { 
     $value .= ';type=' . $this->contentType; 
    } 

    return $value; 
} 
Problemi correlati