Microsoft Graph API - 搜索多个电子邮件地址?

时间:2021-01-30 08:35:33

标签: php curl microsoft-graph-api

我正在尝试使用“参与者”搜索词查询图形 API,以将来自或发送到特定电子邮件地址的邮件消息返回给我。

文档在这里; https://docs.microsoft.com/en-us/graph/query-parameters

代码示例(工作):

$encoded_addresses = urlencode("participants:email@domain.com");
$url = 'https://graph.microsoft.com/v1.0/me/messages?$search="' . $encoded_addresses . '"'; 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Prefer: outlook.body-content-type="text"', 'Accept: application/json','Authorization: Bearer '.$account['access_token']));
$response = curl_exec($ch);
print_r($response);

一个电子邮件地址(上例)有效。但是,如果我将多个电子邮件地址传递给参与者参数,它会失败,并且无论参与者如何,我都会检索邮箱中的所有邮件。

代码示例(不起作用):

$encoded_addresses = urlencode("participants:email@domain.com or anotheremail@anotherdomain.com");
    $url = 'https://graph.microsoft.com/v1.0/me/messages?$search="' . $encoded_addresses . '"'; 
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Prefer: outlook.body-content-type="text"', 'Accept: application/json','Authorization: Bearer '.$account['access_token']));
    $response = curl_exec($ch);
    print_r($response);

我使用 PHP 和 cURL。

有没有人设法搜索多个值?如果是,用什么语法?

注意我使用的是搜索而不是过滤参数。

谢谢

2 个答案:

答案 0 :(得分:1)

正如您在评论中提到的,您的请求网址是这样的:

https://graph.microsoft.com/v1.0/me/messages?$search="participants:simon@bbc.co.uk" or "participants:bob@bbc.co.uk"

这是错误的。正确的形式应该是:

https://graph.microsoft.com/v1.0/me/messages?$search="participants:simon@bbc.co.uk or participants:bob@bbc.co.uk"
https://graph.microsoft.com/v1.0/me/messages?$search="participants:simon@bbc.co.uk or bob@bbc.co.uk"

尝试使用 curl_escape 而不是 urlencode

$ch = curl_init();
$query =  curl_escape($curl ,'participants:email@domain.com or anotheremail@anotherdomain.com');
$url = 'https://graph.microsoft.com/v1.0/me/messages?$search="' . $query . '"'; 

答案 1 :(得分:0)

对于 OR 条件,您可以使用以下方式:

"participants: email@domain.com  or participants:anotheremail@anotherdomain.com"

您的代码修改如下:

$encoded_addresses = urlencode("participants: email@domain.com  or participants:anotheremail@anotherdomain.com");