我知道我可以使用此语法通过 php、post 和 curl 发送文件。
$post = array(
"file_box"=>"@/path/to/myfile.jpg",
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
如何获取字符串,构建临时文件并使用完全相同的语法发送它?
更新:
我更喜欢使用 tmpfile() 或 php://memory ,所以我不必处理文件创建。
最佳答案
您可以使用 tempnam 创建文件在您的临时目录中:
$string = 'random string';
//Save string into temp file
$file = tempnam(sys_get_temp_dir(), 'POST');
file_put_contents($file, $string);
//Post file
$post = array(
"file_box"=>'@'.$file,
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
//do your cURL work here...
//Remove the file
unlink($file);
关于php - 使用 curl 和 php 将字符串作为文件发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6149985/