使用curl POST 二进制文件
我有一个基本的上传表单,我想使用 cURL 来模拟它。
<?php
$params = array(
'api_key' => $api_key,
'api_secret' => $api_secret,
'urls' => null,
'uids' => 'all',
'detector' => 'Aggressive',
'namespace' => 'face.auth');
$action = $url . '?' . http_build_query($params);
?>
<form enctype="multipart/form-data" method="post" action="<?php echo $action; ?>">
<input type="file" name="upload" id="upload">
<input type="submit" />
</form>
我知道如何使用 cURL 发布,但我不确定如何发布图像数据(该数据作为二进制数据存储在数据库中,我们称之为 $binary
)。如下所示传递 $binary
作为后置字段不起作用。我见过一些在文件名/路径前面放置 @
并将其作为邮局字段发送的示例。但是,这似乎对我不起作用(因为我正在处理二进制数据,而不是文件名/路径)。
$params = array(
'api_key' => $api_key,
'api_secret' => $api_secret,
'urls' => null,
'uids' => 'all',
'detector' => 'Aggressive',
'namespace' => 'face.auth',
$binary);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
var_dump($data);
?>
我也尝试过:
$params = array(
'api_key' => $api_key,
'api_secret' => $api_secret,
'urls' => null,
'uids' => 'all',
'detector' => 'Aggressive',
'namespace' => 'face.auth');
$action = $url . '?' . http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $action);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $binary);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
任何帮助将不胜感激。
I have a basic upload form which I would like to emulate using cURL.
<?php
$params = array(
'api_key' => $api_key,
'api_secret' => $api_secret,
'urls' => null,
'uids' => 'all',
'detector' => 'Aggressive',
'namespace' => 'face.auth');
$action = $url . '?' . http_build_query($params);
?>
<form enctype="multipart/form-data" method="post" action="<?php echo $action; ?>">
<input type="file" name="upload" id="upload">
<input type="submit" />
</form>
I know how to post using cURL, but I'm not sure how to post the image data (this data is stored as binary data in a database, lets call it $binary
). Passing $binary
as shown below as a postfield does not work. I have seen some examples which place an @
in front of the file name/path, and send that as a postfield. However, this does not appear to work for me (since I am dealing with binary data, not a file name/path).
$params = array(
'api_key' => $api_key,
'api_secret' => $api_secret,
'urls' => null,
'uids' => 'all',
'detector' => 'Aggressive',
'namespace' => 'face.auth',
$binary);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
var_dump($data);
?>
I've also tried:
$params = array(
'api_key' => $api_key,
'api_secret' => $api_secret,
'urls' => null,
'uids' => 'all',
'detector' => 'Aggressive',
'namespace' => 'face.auth');
$action = $url . '?' . http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $action);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $binary);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
Any assistance would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不需要像 @Piskvor 在 中构建的那样构建完整的数据字符串在 PHP 中使用 cURL 发布文件字符串? 来实现这一点...我们可以通过在数组中传递二进制数据来实现这一点...
因为curl 在内部执行相同的字符串构建“Piskvor”所做的事情在上面的问题中......
当curl获取数组时在postfield中,curl将该数据视为“multipart/form-data”,
但为了实现这一点,我们只需要在传递二进制数据的数组键中做一些小修复...请检查下面,您需要传递如下的binaryData。 ..瞧,您将在远程 url 上的 $_files 数组中得到它
现在,它是如何实现的:
请告诉我您的代码中的上述小更改是否不起作用...因为它对我有用...我喜欢分享这个...
It is not necessary to build complete data string like what @Piskvor has built in POST a file string using cURL in PHP? to achieve this...we could do this by just passing binary data in array it-self...
As curl internally does the same string building what "Piskvor" has done in above problem...
As and when curl gets array in postfield curl treats that data as "multipart/form-data"
but to achieve this we just need to do small fix in array key in which we are passing binary data...please check below you need to pass binaryData as below....and voila you will get this in $_files array on remote url
Now, how it was achieved:
please let me know if above small change in your code doesn't works....as it worked for me...I like to share this...