How to get all the groups for a contact
To get the Groups, you can use the GroupContact API

$result = civicrm_api3('GroupContact', 'get', array(
  'sequential' =&gt; 1,
  'contact_id' =&gt; &lt;contact_id&gt;,
));


To get all the smart groups to which the contact belongs, you can use this BAO function defined in GroupContactCache.php

$smartGroup = CRM_Contact_BAO_GroupContactCache::contactGroup($contactId);


returned $smartGroup will be the list of smart_groups for the contactId passed