使用curl和php作为文件发送字符串 [英] Send string as a file using curl and php
本文介绍了使用curl和php作为文件发送字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道我可以使用这个语法使用php,post和curl发送一个文件。
$ post = array
file_box=>@ / path / to / myfile.jpg,
);
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ post);
如何获取字符串,构建临时文件并使用完全相同的语法发送? / p>
更新:
我更喜欢使用tmpfile()或php://内存,所以我不必处理文件创建。
解决方案
您可以使用 tempnam 在您的临时目录中:
$ string ='random string'
//将字符串保存到临时文件
$ 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);
//你的cURL在这里工作...
//删除文件
unlink($ file);
I know I can use this syntaxt to send a file using php, post and curl.
$post = array(
"file_box"=>"@/path/to/myfile.jpg",
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
How can I take a string, build a temp file and send it using the exact same syntax ?
Update: I would prefer using tmpfile() or php://memory so I don't have to handle file creation.
解决方案
You can create a file using tempnam in your temp directory:
$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);
这篇关于使用curl和php作为文件发送字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文