How can I Download/Access SharePoint Online Documents using PHP / XML / SOAP?

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I am working on a web project that involves connecting to SharePoint Online via PHP and accessing the files stored on it. But I am extremely new to all this, and have hit a wall.

  • I have the URL of the file I'm trying to access
  • Using the phpSPO library, I am authenticated in SharePoint.

If I follow the link to that URL, I am redirected to SharePoint's login page. But we don't want that to happen - we want the file to open up for our users without further authentication.

The company we're working with said we could call a function from SharePoint and request a temporary anonymous link for the file we want. But we are working in PHP, and that function doesn't appear to be accessible to us.

Here's the ASPX code they sent us:

Uri siteUri = new Uri(siteUrl);
Web web = context.Web;
SecureString passWord = new Secure String();
foreach (char c in "password".ToCharArray())
    passWord.AppendChar(c);
context.Credentials = new SharePointOnlineCredentials("userid", passWord);
WebDocs.Parameter1 = "123456"
WebDocs.Parameter2 = "Test"
context.Web.CreateAnonymousLinkForDocument(WebDocs.Parameter1, 
WebDocs.Parameter2, ExternalSharingDocumentOption.View);

But how can I translate that into PHP?

// this says the function CreateAnonymousLinkForDocument doesn't exist
function getLink(ClientContext $ctx) {
    $anonymousLink = $ctx->getWeb()->CreateAnonymousLinkForDocument(); 
    $ctx->load($anonymousLink);
    $ctx->executeQuery();
}

So in short, is there a way that I can allow our users to access the files in SharePoint? Either by requesting an anonymous link, or downloading a copy of the file onto our own server temporarily, reading it into a binary file, whatever?

I have searched the Internet for over a week, and have not found anything helpful to someone programming in PHP, at least that specifically addresses our question.

profilepic.png
manpreet 2 years ago

Well, after hours and hours of searching the Internet....

The answer was right in front of my nose.

Started browsing through the examples/SharePoint/file_examples.php that came with the phpSPO library, and discovered 2 functions (either one works).

One is called downloadFile, and the other is downloadFileAsStream.

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";
}

Since I was trying to download a PDF, I just set these functions to create a PDF on our own server.... and it works beautifully!!!!!


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.