开发者

POST binary file using curl

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="up开发者_高级运维load">
    <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.


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜