2016-08-15 125 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