2017-05-11 33 views
0

我正在開發一個涉及通過PHP連接到SharePoint Online並訪問存儲在其上的文件的Web項目。但是,我對這一切都非常陌生,並且遇到了困難。使用PHP/XML/SOAP下載或訪問SharePoint Online文檔

  • 我有文件的URL我試圖訪問
  • 使用phpSPO library,我驗證和連接到SharePoint。

問題是:我該如何實際訪問URL?如果我直接關注該鏈接,它會將我重定向到SharePoint的登錄頁面。但是我們希望登錄是在「幕後」進行的 - 顯然,身份驗證步驟並沒有這樣做。

我們正在合作的公司告訴我們,我們需要通過調用函數來請求URL的匿名鏈接。問題是,他們告訴我們使用ASPX的功能,但似乎沒有在PHP中可用。

這是他們指着我們的代碼:

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

但我怎麼能翻譯成PHP?我可以這樣做嗎?

如果沒有,是否有另一種方式可以訪問該文件以將其顯示給我的用戶?

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

回答

1

好了,經過時間和搜索互聯網的時間....

答案就在我的鼻子前面。

開始瀏覽phpSPO庫附帶的examples/SharePoint/file_examples.php文件,並發現了2個函數(可以工作)。

一個叫做downloadFile,另一個叫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"; 
} 

因爲我試圖下載一個PDF,我只設置這些功能對我們自己的服務器上創建一個PDF ....和它的作品精美!!!!!

相關問題