Search code examples
phpoutlookmicrosoft-graph-apimicrosoft-graph-sdksmicrosoft-graph-mail

how to get value of contentBytes from microsoft graph sdk data response in php


My code is

$expand="microsoft.graph.itemattachment/item";
            $requestUrl = '/me/messages/'.$message->getId().'/attachments/?$expand=  '.$expand;
            $docDatas = $userClient->createCollectionRequest('GET', $requestUrl)
                                    ->setReturnType(Model\Message::class)
                                    ->setPageSize(1)
                                      ->getPage();

And want to get the outlook email attachment

Please help my

we get responses like this

Microsoft\Graph\Model\Message Object
(
    [_propDict:protected] => Array
        (
            [@odata.type] => #microsoft.graph.fileAttachment
            [@odata.mediaContentType] => application/pdf
            [id] => AAMkADE2M2FjZjMyLTY2YjAtNDQwZi1hMzc3LTI2MjYwNmQ0NTJhYwBGAAAAAAB13byROi0bTImrZtPU6w6LBwAIBVrWv6bqRZ6zXuSjdaiOAAAAAAEMAAAIBVrWv6bqRZ6zXuSjdaiOAAUI9pGdAAABEgAQABzAnt_evzNMiFTD_ANAZno=
            [lastModifiedDateTime] => 2022-06-28T05:54:14Z
            [name] => test.pdf
            [contentType] => application/pdf
            [size] => 66087
            [isInline] => 
            [contentId] => [email protected]
            [contentLocation] => 
            [contentBytes] => JVBERi0xLjQKJe...
        )

)

See Attachment

I want to get value contentBytes but how? Again get error enter image description here


Solution

  • You could either extend Message with a CustomMessage class and than have a specific getter for that property if you'll use it extensively. My assumption was that you use msgraph-sdk-php library.

    Other, simpler solution would be to just:

    $expand="microsoft.graph.itemattachment/item";
    $requestUrl = '/me/messages/'.$message->getId().'/attachments/?$expand=  '.$expand;
    $docDatas = $userClient->createCollectionRequest('GET', $requestUrl)
                           ->setReturnType(Model\Message::class)
                           ->setPageSize(1)
                           ->getPage();
    
    $properties = $docDatas->getProperties();
    $contentBytes = $properties["contentBytes"];
    

    Message extends OutlookMessage which extends Microsoft\Graph\Model\Entity. You can find getProperties method in that class.