Any example on how to use the OCS Share API to share a file with php?

Hi, my client is using Nextcloud, and they asked me to send temporary file sharing links to their website users when they ask for those files. Therefore, I suppose need to use the OCS Share API. I’m a php programmer. Is there any php example code on using that API? Can’t find on the web or NC OCS Share API docs. Any place where I can find a starting point? Thank you

I found this one but it is a bit old:

and it is nothing official. But perhaps to get some ideas. And there is probably more out there. As far as I know there is no official reference implementation …

Thank you. Also, ChatGPT gave me this:


$nextcloudUrl = '';
$username = 'il-tuo-nome-utente';
$password = 'la-tua-password';

$fileToShare = '/percorso/al/tuo/file.txt';

$expiration = 3 * 24 * 60 * 60; // 3 giorni

$response = createTemporaryShare($nextcloudUrl, $username, $password, $fileToShare, $expiration);


function createTemporaryShare($url, $username, $password, $file, $expiration) {
    $requestData = array(
        'shareType' => 0, // Public share
        'path' => $file,
        'permissions' => 1, // Read only
        'expireDate' => time() + $expiration

    $ch = curl_init($url . '/ocs/v2.php/apps/files_sharing/api/v1/shares');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($requestData));
    curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);

    return $response;


Maybe not perfect, but I’ll also try this.