I need to download a file in SharePoint Online from PHP.
The only data I have is the URL of the folder.
Access is public, does not require authentication.
How can you complete this task from PHP? Any API?
Thanks in advance.
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityI need to download a file in SharePoint Online from PHP.
The only data I have is the URL of the folder.
Access is public, does not require authentication.
How can you complete this task from PHP? Any API?
Thanks in advance.
I struggled with the same issue using phpSPO, found a direct way to download. Simply replace everything after the ? in the url with "download=1" src
$fileUrl = "https://mycompany.sharepoint.com/folders/someGUIDlikeSTRING?download=1";
$filePath = "/tmp/saveHere.file";
$strCmd = "wget -O " . $filePath . " " . $fileUrl;
shell_exec($strCmd);
Curl kept receiving a 403, while wget worked fine. Its a hackish shell call, obviously curl would be a better solution, but it works.
You can use phpSPO library (https://github.com/vgrem/phpSPO). It has 2 methods which can be used. below is snippet from another post on stackoverflow
function downloadFile(ClientRuntimeContext $ctx, $fileUrl, $targetFilePath){
$fileContent =
Office365\PHP\Client\SharePoint\File::openBinary($ctx,$fileUrl);
file_put_contents($targetFilePath, $fileContent);
print "File {$fileUrl} has been downloaded successfully\r\n";
}
function downloadFileAsStream(ClientRuntimeContext $ctx, $fileUrl,
$targetFilePath) {
$fileUrl = rawurlencode($fileUrl);
$fp = fopen($targetFilePath, 'w+');
$url = $ctx->getServiceRootUrl() . "web/getfilebyserverrelativeurl('$fileUrl')/\$value";
$options = new \Office365\PHP\Client\Runtime\Utilities\RequestOptions($url);
$options->StreamHandle = $fp;
$ctx->executeQueryDirect($options);
fclose($fp);
print "File {$fileUrl} has been downloaded successfully\r\n";
}
Ref link