How to download submission attachments from the REST API

With further trial, i can now download the attachment using copy(), here is a sample code:

<?php


$source = "https://kc.kobotoolbox.org/media/small?media_file=youssouf_traore%2Fattachments%2Faeacc024$
$target = "topo.jpg"; //File destination name
file_put_contents($target, ""); //We have to create the destination file prior to using copy().

 //Options on the request:
 $options = array(
"http" => array(
    "method"=> "GET",
    "header" => "Authorization: Basic "  . base64_encode("youssouf_traore:my-secret-password")
    )
 );
$streamContext = stream_context_create($options);
copy($source, $target, $streamContext);

?>
2 Likes