phpSPO
phpSPO copied to clipboard
Failure of download file from sharepoint
I installed phpSPO library in Windows server and want to download a file from Sharepoint Documents folder, which needs credential to login. I use user credentials auth to get the file, but it shows the error.
============ Fatal error: Uncaught Error: Class 'AuthenticationContext' not found in C:\xxx\xxx\xxx\xxx\DownloadFile.php:16 Stack trace: #0 {main} thrown in C:\xxx\xxx\xxx\xxx\DownloadFile.php on line 16
Codes in DownloadFile.php
acquireTokenForUser($username,$password); $ctx = new ClientContext($url,$authCtx); $fileUrl = 'https://test.sharepoint.com/sites/test/'; $sourceLibraryTitle = "Documents"; $sourceList = $ctx->getWeb()->getLists()->getByTitle($sourceLibraryTitle); //$files = $sourceList->getRootFolder()->getFiles(); $files = array("fileabc.xlsx"); $ctx->load($files); $ctx->executeQuery(); /** @var File $file */ foreach ($files->getData() as $file){ try { $temp_file = join(DIRECTORY_SEPARATOR,[sys_get_temp_dir(),$file->getName()]); $result = $file->download(); $ctx->executeQuery(); file_put_contents($targetFilePath, $result->getValue()); print "File {$file->getServerRelativeUrl()} has been downloaded successfully\r\n"; } catch (Exception $e) { print "File download failed:\r\n"; } } Please help for this. Thanks
Hello
Sound a little that you are just missing something on top of the file.
Do you have these lines?
Hello,
2.3.1 to 2.4 changed namespace.
From:
namespace Office365\PHP\Client\Runtime\Auth;
To:
namespace Office365\Runtime\Auth;
Is that maybe the cause of your errors?