PHP cURL:如何将正文设置为二进制数据? [英] PHP cURL: how to set body to binary data?
本文介绍了PHP cURL:如何将正文设置为二进制数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用一个 API,它希望我发送一个 POST,其中包含来自文件的二进制数据作为请求的正文.如何使用 PHP cURL 完成此操作?
I'm using an API that wants me to send a POST with the binary data from a file as the body of the request. How can I accomplish this using PHP cURL?
与我试图实现的等效的命令行是:
The command line equivalent of what I'm trying to achieve is:
curl --request POST --data-binary "@myimage.jpg" https://myapiurl
推荐答案
你可以在 CURLOPT_POSTFIELDS
中设置你的 body.
You can just set your body in CURLOPT_POSTFIELDS
.
示例:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://url/url/url" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, "body goes here" );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
$result=curl_exec ($ch);
取自此处
当然,设置你自己的header类型,只需要为body做file_get_contents('/path/to/file')
即可.
Of course, set your own header type, and just do file_get_contents('/path/to/file')
for body.
这篇关于PHP cURL:如何将正文设置为二进制数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文