【发布时间】:2021-02-10 11:57:50
【问题描述】:
问题 我正在尝试创建一个新的 docx 文件,其中包含一些文本但无法传递数据,因为没有示例如何在 MS Graph API 中上传实际文件
不知道要在正文中附加哪些变量以用于内容和二进制流。如果我注释 attachBody() 参数,它将仅通过在文档文件夹的根目录中创建一个文本文件来工作。
$graph = new Graph();
$graph->setAccessToken($accessToken);
$content = [
'Content-Type: text/plain
The contents of the file goes here.'
];
$data = $graph->createRequest('PUT', '/sites/09e0add7-675f-4694-a1d9-999be420a807/drives/b!163gCV9nlEah2Zmb5CCoBwohbFPtU9lDr3_IcXUI8qiDyOuVQMhDSohSZRBTS6YL/root:/test.docx:/content')
->attachBody($content)
->setReturnType(Model\User::class)
->execute();
问题已更新 将文本/文档/图像/数据转换为二进制流然后执行请求
代码
$text_data=file_get_contents('C:\Users\Lab1-WS-5\Downloads/demo.docx');
$encoded_text_data=base64_encode($text_data);
// ------------------------------------------------------------------------
$graph = new Graph();
$graph->setAccessToken($accessToken);
// $content = 'The contents of the file goes here.';
// $encoded = base64_encode($content);
$data = $graph->createRequest('PUT', '/sites/09e0add7-675f-4694-a1d9-999be420a807/drives/b!163gCV9nlEah2Zmb5CCoBwohbFPtU9lDr3_IcXUI8qiDyOuVQMhDSohSZRBTS6YL/root:/charlie.docx:/content')
->addHeaders(array('Content-Type' => 'text/plain'))
->attachBody($encoded_text_data)
->setReturnType(Model\User::class)
->execute();
当前问题使用图形 api 创建的文档未打开请求权限,而手动上传的文件则没有。
【问题讨论】:
-
您可以尝试使用
$image_data=file_get_contents('test.jpg'); $encoded_image=base64_encode($image_data);对于文本文件,您可以通过将 Content-Type 标头设置为 text/plain 来直接使用数据 -
@Shiva-MSFTIdentity 检查我的 api 查询它创建文档但无法打开,而如果你手动上传 docx 文件它打开没有任何错误
-
创建的文件是base64的,如果你下载的文件有base64代码也不能打开
标签: php microsoft-graph-api codeigniter-3 sharepoint-api codeigniter-4