Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

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

General Tech Bugs & Fixes

Max. 2000 characters
Replies

usr_profile.png

User

( 5 months 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.

usr_profile.png

User

( 5 months 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!!!!!

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif