Microsoft graph api ms graph php sdk消息对象无法检查是否存在附件

Microsoft graph api ms graph php sdk消息对象无法检查是否存在附件,microsoft-graph-api,microsoft-graph-sdks,microsoft-graph-mail,Microsoft Graph Api,Microsoft Graph Sdks,Microsoft Graph Mail,我正在从Outlook帐户检索邮件。我正试图从这些邮件中获取内联文件和附件 $graph = new Graph(); $graph->setAccessToken($this->getAccessToken()); $messageQueryParams = array ( "\$select" => "subject,receivedDateTime,from,sentDateTime,body,toRecipients,sender,uniqueBody,ccR

我正在从Outlook帐户检索邮件。我正试图从这些邮件中获取内联文件和附件

$graph = new Graph();
$graph->setAccessToken($this->getAccessToken());

$messageQueryParams = array (
    "\$select" => "subject,receivedDateTime,from,sentDateTime,body,toRecipients,sender,uniqueBody,ccRecipients,bccRecipients,attachments",
    "\$orderby" => "receivedDateTime DESC",
    "\$top" => "200" 
);

$url = '/me/mailfolders/' . $folder . '/messages/delta';
$url_combiner = '?';

$getMessagesUrl = $url . $url_combiner . http_build_query($messageQueryParams);
$response = $graph->createRequest('GET', $getMessagesUrl)->execute();

$messages = $response->getResponseAsObject( \Microsoft\Graph\Model\Message::class );

foreach($messages as $msg) {
     echo $msg->getHasAttachments();
}
此代码为$msg->getHasAttachments()返回“null”;我希望它能返回一个正确或错误的答案

我从这个文件夹下载的邮件既有内联附件也有邮件附件,所以我正在寻找解决这两个问题的方法


(非常感谢指向MS Graph PHP SDK文档中特定点的响应。)

在您的情况下,这是预期的行为
getHasAttachments()
方法返回
null
,因为
hasAttachments
未包含在
$select
查询选项中,因此不会从服务器请求它。需要明确包括,例如:

$messageQueryParams = array (
    "\$select" => "hasAttachments,...",
    //another params are omitted for clarity
);
然后,邮件是否包含附件可以这样确定:

foreach($messages as $msg) {
    if($msg->getHasAttachments() == true){
        //...
}
$messageQueryParams = array (
     "\$expand" => "attachments",
    //another params are omitted for clarity
); 
 foreach($messages as $msg) {
    foreach($msg->getAttachments() as $attachment) {
        //...
    }
 }
所提供的示例还有一个问题,要检索
邮件。附件
引用属性(或关系属性),需要通过
$expand
查询字符串参数指定它,而不是
$select
(请参阅以获取更多详细信息),如下所示:

foreach($messages as $msg) {
    if($msg->getHasAttachments() == true){
        //...
}
$messageQueryParams = array (
     "\$expand" => "attachments",
    //another params are omitted for clarity
); 
 foreach($messages as $msg) {
    foreach($msg->getAttachments() as $attachment) {
        //...
    }
 }
然后可以像这样迭代attachments集合:

foreach($messages as $msg) {
    if($msg->getHasAttachments() == true){
        //...
}
$messageQueryParams = array (
     "\$expand" => "attachments",
    //another params are omitted for clarity
); 
 foreach($messages as $msg) {
    foreach($msg->getAttachments() as $attachment) {
        //...
    }
 }