PHP cURL:如何将body设置为二进制数据? [英] PHP cURL: how to set body to binary data?

查看:4697
本文介绍了PHP cURL:如何将body设置为二进制数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个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 中设置您的身体。

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);

取自在这里

当然,设置你自己的头类型,只需做 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:如何将body设置为二进制数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆