Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Bugs & Fixes 2 years ago
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.
Turn Your Knowledge into Earnings.
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.
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.
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!!!!!
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.
General Tech 10 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 9 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.