Note that the $postname and $mimetype parameters are in the reverse order to the CURLFile::__construct function.(PHP 8 >= 8.1.0)
CURLStringFile::__construct — Cria um objeto CURLStringFile
$data, string $postname, string $mime = "application/octet-stream")
Cria um objeto CURLStringFile, usado para carregar um arquivo com CURLOPT_POSTFIELDS.
dataO conteúdo a ser carregado.
postnameO nome do arquivo a ser usado nos dados de carregamento.
mime
Tipo MIME do arquivo (o padrão é application/octet-stream).
Exemplo #1 Exemplo de CURLStringFile::__construct()
<?php
/* http://example.com/upload.php:
<?php
var_dump($_FILES);
var_dump(file_get_contents($_FILES['test_string']['tmp_name']));
?>
*/
// Cria um identificador cURL
$ch = curl_init('http://example.com/upload.php');
// Cria um objeto CURLStringFile
$cstringfile = new CURLStringFile('test upload contents','test.txt','text/plain');
// Atribui dados POST
$data = array('test_string' => $cstringfile);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// Executa o manipulador
curl_exec($ch);
?>O exemplo acima produzirá:
array(1) {
["test_string"]=>
array(5) {
["name"]=>
string(8) "test.txt"
["type"]=>
string(10) "text/plain"
["tmp_name"]=>
string(14) "/tmp/phpTtaoCz"
["error"]=>
int(0)
["size"]=>
int(20)
}
}
string(20) "test upload contents"