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())
context.Credentials = new SharePointOnlineCredentials("userid", passWord);
WebDocs.Parameter1 = "123456"
WebDocs.Parameter2 = "Test"
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(); 

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.

1 Answer 1


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 = 
    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;

    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!!!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.