vgrem / phpSPO

Microsoft 365 Library for PHP.
MIT License
360 stars 117 forks source link

Failure of download file from sharepoint #193

Open joannelfm opened 4 years ago

joannelfm commented 4 years ago

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

<?php

use Office365\SharePoint\ClientContext; use Office365\SharePoint\File;

require_once '../../vendor/autoload.php';

function downloadFileAlt(){ //$fileContent = Office365\SharePoint\File::openBinary($ctx, $fileUrl); }

$url = "https://test.sharepoint.com/sites/test/"; $username = "name@name.com"; $password = "xxxx";

$authCtx = new AuthenticationContext($url); $authCtx->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

hnpilot commented 4 years ago

Hello

Sound a little that you are just missing something on top of the file.

Do you have these lines?

<?php require_once 'vendor/autoload.php'; use Office365\SharePoint\ClientContext;

wbr hank

fdverwoerd commented 4 years ago

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?