使用curl POST 二进制文件

发布于 2024-11-28 08:13:23 字数 1832 浏览 0 评论 0原文

我有一个基本的上传表单,我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笨笨の傻瓜 2024-12-05 08:13:23

不需要像 @Piskvor 在 中构建的那样构建完整的数据字符串在 PHP 中使用 cURL 发布文件字符串? 来实现这一点...我们可以通过在数组中传递二进制数据来实现这一点...

因为curl 在内部执行相同的字符串构建“Piskvor”所做的事情在上面的问题中......

当curl获取数组时在postfield中,curl将该数据视为“multipart/form-data”,

但为了实现这一点,我们只需要在传递二进制数据的数组键中做一些小修复...请检查下面,您需要传递如下的binaryData。 ..瞧,您将在远程 url 上的 $_files 数组中得到它

$params['image";filename="image'] = $binaryData

现在,它是如何实现的:

仅当远程服务器在由curl构建的post字符串中获取filename =“some_name”属性时,远程服务器才能识别您的文件的post数据....curl会在@filepath的情况下自动添加此属性,但是当您将其作为二进制数据传递时,它不会不明白它是文件...因此我们在 post 数组而不是文件数组中获得上述二进制内容...

请告诉我您的代码中的上述小更改是否不起作用...因为它对我有用...我喜欢分享这个...

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

$params['image";filename="image'] = $binaryData

Now, how it was achieved:

Remote server recognize your post data for file only if it gets filename="some_name" attribute in post string built by curl....curl automatically adds this in case of @filepath but when you pass it as binary data it doesn't understands that it is file...so by that we get above binary content in post array instead of file array....

please let me know if above small change in your code doesn't works....as it worked for me...I like to share this...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文