【问题标题】:Download or Access to SharePoint Online Documents using PHP / XML / SOAP使用 PHP/XML/SOAP 下载或访问 SharePoint Online 文档
【发布时间】:2017-05-16 04:05:45
【问题描述】:

我正在处理一个 Web 项目,该项目涉及通过 PHP 连接到 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();
}

【问题讨论】:

    标签: php xml sharepoint soap token


    【解决方案1】:

    好吧,经过数小时的互联网搜索......

    答案就在我眼前。

    开始浏览 phpSPO 库附带的示例/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....而且效果很好!!!!!!

    【讨论】:

      猜你喜欢
      • 2016-07-23
      • 2017-02-28
      • 1970-01-01
      • 2020-06-16
      • 2013-09-22
      • 1970-01-01
      • 1970-01-01
      • 2019-02-08
      • 1970-01-01
      相关资源
      最近更新 更多