在V1中,我可以

$this->graph->createRequest('GET', '/drives/' . $driveId . '/items/' . $itemId . '/content')->download($path);

在V2中如何做到这一点?

我认为这已经很接近了,但我想不出如何在$Response中写入文件.

$tokenRequestContext = new \Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext(
    config('graphapi.azure_tenant_id'),
    config('graphapi.azure_client_id'),
    config('graphapi.azure_client_secret')
);

$graphServiceClient = new \Microsoft\Graph\GraphServiceClient($tokenRequestContext);

$response  = $graphServiceClient->drives()
    ->byDriveId(config('graphapi.attachments_drive_id'))
    ->items()
    ->byDriveItemId(<drive_item_id>)
    ->content()
    ->get();

编辑:

$Response返回promise :

object(Http\Promise\FulfilledPromise)#5993 (1) {
  ["result":"Http\Promise\FulfilledPromise":private]=>
  object(GuzzleHttp\Psr7\Stream)#6066 (7) {
    ["stream":"GuzzleHttp\Psr7\Stream":private]=>
    resource(965) of type (stream)
    ["size":"GuzzleHttp\Psr7\Stream":private]=>
    NULL
    ["seekable":"GuzzleHttp\Psr7\Stream":private]=>
    bool(true)
    ["readable":"GuzzleHttp\Psr7\Stream":private]=>
    bool(true)
    ["writable":"GuzzleHttp\Psr7\Stream":private]=>
    bool(true)
    ["uri":"GuzzleHttp\Psr7\Stream":private]=>
    string(10) "php://temp"
    ["customMetadata":"GuzzleHttp\Psr7\Stream":private]=>
    array(0) {
    }
  }
}

因此,我将代码更改为:

$promise  = $graphServiceClient->drives()
    ->byDriveId(config('graphapi.attachments_drive_id'))
    ->items()
    ->byDriveItemId(<drive_item_id>)
    ->content()
    ->get();

$promise->then(
    function ($response) {
        $fileContent = $response->getBody()->getContents();
        file_put_contents('test.txt', $fileContent);
    },
    function ($exception) {
        echo "Error: " . $exception->getMessage();
    }
);

但我收到错误:对未定义的方法GuzzleHttp\Psr7\Stream::getBody()的错误调用.

推荐答案

对于任何遇到这种情况并试图使用PHPSDK的V2的人,这里有一个解决方案.

$tokenRequestContext = new \Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext(
    config('graphapi.azure_tenant_id'),
    config('graphapi.azure_client_id'),
    config('graphapi.azure_client_secret')
);

$graphServiceClient = new \Microsoft\Graph\GraphServiceClient($tokenRequestContext);

$stream = $graphServiceClient->drives()
    ->byDriveId(config('graphapi.attachments_drive_id'))
    ->items()
    ->byDriveItemId(<drive_item_id>)
    ->content()
    ->get()
    ->wait();

file_put_contents('/path/file_name.txt', $stream);

新doctor 来了:https://learn.microsoft.com/en-us/graph/api/overview?view=graph-rest-1.0

驱动器项目下载:https://learn.microsoft.com/en-us/graph/api/driveitem-get-content?view=graph-rest-1.0&tabs=php#request

Php相关问答推荐

419注册期间Laravel 11中的错误

使用额外的输入字段自定义WooCommerce单个产品

PHP FFI—Convert void * to int

无法使用DOMPDF在PDF中呈现非ANSI字符

PHP:从数组中获取唯一的数字组合

Laravel集合块()方法

将变体设置字段添加到WooCommerce中的特定变量产品

WordPress插件和JSON不是有效的响应错误

WHERE方法不能与有效查询Laravel一起使用

如果为布尔值,则 for each 循环重新启动

Rappasoft Datatables V2(我想在另一个表具有相同值列时显示数据)

按日期向 WooCommerce 中的我的帐户订单添加过滤器

需要以一种形式执行两个操作

在 Heroku 上部署 Sylius - 路由不正确

如何判断php中wordpress路径之外的文件夹是否存在?

尽管有use指令,PHP 类在其他文件中仍处于可见状态

如何搜索和替换 XML 文件中的一段文本?

Composer自动加载器重复了子类的类文件路径

标头内容类型:image/jpeg 无效

getenv() 有时在 CodeIgniter 4 中返回 false.为什么?