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:
https://github.com/ovesco/nextcloud-api-wrapper
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:
<?php
$nextcloudUrl = 'https://tuo-server-nextcloud.com';
$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);
var_dump($response);
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_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
?>
Maybe not perfect, but I’ll also try this.