2016-08-15 89 views
1

Google最近发布了其新版Gmail API,现在可以使用create filters使用Gmail API创建过滤器

但是,文档是相当有限的,我面临的问题,以使其工作。我正在使用PHP client的最新版本。任何帮助将不胜感激构建请求主体。

public $gmail; 
public function createFilter($userId) { 

    try { 

     $filter = new Google_Service_Gmail_Resource_UsersSettingsFilters(); 
     // Here, we should create the request body... 
     // https://developers.google.com/gmail/api/v1/reference/users/settings/filters#resource 
     // $filter->setCriteria() ?? 

     $this->gmail->users_settings_filters->create($userId, $filter); 


    } catch (Exception $e) { 
     // Logging errors... 
    } 

} 

UPDATE(加工方法)

public $gmail; 
public function createFilter($userId) { 

    try { 

     $filter = new Google_Service_Gmail_Filter([ 
      'criteria' => [ 
       'from' => '[email protected]' 
      ], 
      'action' => [ 
       'addLabelIds' => ['STARRED'] 
      ] 
     ]); 

     $this->gmail->users_settings_filters->create($userId, $filter); 


    } catch (Exception $e) { 
     // Logging errors... 
    } 

} 

回答

1

指导参见https://github.com/google/google-api-php-client#making-requests上构建的请求对象。您应该能够使用本机PHP数组或自动生成的对象填充筛选器属性。例如:

$filter = new Google_Service_Gmail_Resource_UsersSettingsFilters([ 
    'criteria' => [ 
     'from' => '[email protected]' 
    ], 
    'action' => [ 
     'addLabelIds' => ['STARRED'] 
    ] 
]); 
+0

非常感谢@Brandon。我用一个工作解决方案更新了我的问题。 – flo