PHP Extension GMail: List all GMail Labels

Back to Index

Lists all labels in the user's mailbox.

Documentation: https://developers.google.com/gmail/api/v1/reference/users/labels/list

CURL Command

curl -X GET https://www.googleapis.com/gmail/v1/users/me/labels \
    --header "Authorization: Bearer GMAIL_TOKEN"

PHP Extension Example

<?php

// The version number (9_5_0) should match version of the Chilkat extension used, omitting the micro-version number.
// For example, if using Chilkat v9.5.0.48, then include as shown here:
include("chilkat_9_5_0.php");

$rest = new CkRest();

//  URL: https://www.googleapis.com/gmail/v1/users/me/labels
$bTls = true;
$port = 443;
$bAutoReconnect = true;
$success = $rest->Connect('www.googleapis.com',$port,$bTls,$bAutoReconnect);
if ($success != true) {
    print 'ConnectFailReason: ' . $rest->get_ConnectFailReason() . "\n";
    print $rest->lastErrorText() . "\n";
    exit;
}

$rest->AddHeader('Authorization','Bearer GMAIL_TOKEN');

$sbResponseBody = new CkStringBuilder();
$success = $rest->FullRequestNoBodySb('GET','/gmail/v1/users/me/labels',$sbResponseBody);
if ($success != true) {
    print $rest->lastErrorText() . "\n";
    exit;
}

$respStatusCode = $rest->get_ResponseStatusCode();
if ($respStatusCode >= 400) {
    print 'Response Status Code = ' . $respStatusCode . "\n";
    print 'Response Header:' . "\n";
    print $rest->responseHeader() . "\n";
    print 'Response Body:' . "\n";
    print $sbResponseBody->getAsString() . "\n";
    exit;
}

$jsonResponse = new CkJsonObject();
$jsonResponse->LoadSb($sbResponseBody);

//  See the Online Tool for Generating JSON Parse Code

$i = 0;
$count_i = $jsonResponse->SizeOfArray('labels');
while ($i < $count_i) {
    $jsonResponse->put_I($i);
    $id = $jsonResponse->stringOf('labels[i].id');
    $name = $jsonResponse->stringOf('labels[i].name');
    $messageListVisibility = $jsonResponse->stringOf('labels[i].messageListVisibility');
    $labelListVisibility = $jsonResponse->stringOf('labels[i].labelListVisibility');
    $type = $jsonResponse->stringOf('labels[i].type');
    $i = $i + 1;
}


?>

Sample JSON Response Body

{
  "labels": [
    {
      "id": "Label_5",
      "name": "QA",
      "messageListVisibility": "show",
      "labelListVisibility": "labelShow",
      "type": "user"
    },
    {
      "id": "Label_21",
      "name": "[Gmail]/testFolder",
      "type": "user"
    },
    {
      "id": "CATEGORY_PERSONAL",
      "name": "CATEGORY_PERSONAL",
      "type": "system"
    },
    {
      "id": "Label_8",
      "name": "old",
      "type": "user"
    },
    {
      "id": "CATEGORY_SOCIAL",
      "name": "CATEGORY_SOCIAL",
      "type": "system"
    },
    {
      "id": "Label_7",
      "name": "labél with space",
      "type": "user"
    },
    {
      "id": "Label_41",
      "name": "QA/qa_fetchSingleAsMime",
      "type": "user"
    },
    {
      "id": "Label_4",
      "name": "[Imap]/Trash",
      "messageListVisibility": "hide",
      "labelListVisibility": "labelShow",
      "type": "user"
    },
    {
      "id": "Label_1",
      "name": "\"test\"",
      "messageListVisibility": "hide",
      "labelListVisibility": "labelShow",
      "type": "user"
    },
    {
      "id": "Label_6",
      "name": "QA/Entwürfe",
      "messageListVisibility": "hide",
      "labelListVisibility": "labelShow",
      "type": "user"
    },
    {
      "id": "CATEGORY_FORUMS",
      "name": "CATEGORY_FORUMS",
      "type": "system"
    },
    {
      "id": "Label_42",
      "name": "[Gmail]/フルタイムの共有ソリューションはjapan",
      "type": "user"
    },
    {
      "id": "Label_3",
      "name": "[Gmail]/ö",
      "messageListVisibility": "hide",
      "labelListVisibility": "labelShow",
      "type": "user"
    },
    {
      "id": "IMPORTANT",
      "name": "IMPORTANT",
      "messageListVisibility": "hide",
      "labelListVisibility": "labelShow",
      "type": "system"
    },
    {
      "id": "Label_2",
      "name": "[Gmail]/X&Y",
      "type": "user"
    },
    {
      "id": "CATEGORY_UPDATES",
      "name": "CATEGORY_UPDATES",
      "type": "system"
    },
    {
      "id": "CHAT",
      "name": "CHAT",
      "messageListVisibility": "hide",
      "labelListVisibility": "labelShow",
      "type": "system"
    },
    {
      "id": "SENT",
      "name": "SENT",
      "messageListVisibility": "hide",
      "labelListVisibility": "labelShow",
      "type": "system"
    },
    {
      "id": "INBOX",
      "name": "INBOX",
      "messageListVisibility": "hide",
      "labelListVisibility": "labelShow",
      "type": "system"
    },
    {
      "id": "TRASH",
      "name": "TRASH",
      "messageListVisibility": "hide",
      "labelListVisibility": "labelHide",
      "type": "system"
    },
    {
      "id": "CATEGORY_PROMOTIONS",
      "name": "CATEGORY_PROMOTIONS",
      "type": "system"
    },
    {
      "id": "DRAFT",
      "name": "DRAFT",
      "messageListVisibility": "hide",
      "labelListVisibility": "labelShow",
      "type": "system"
    },
    {
      "id": "SPAM",
      "name": "SPAM",
      "messageListVisibility": "hide",
      "labelListVisibility": "labelHide",
      "type": "system"
    },
    {
      "id": "STARRED",
      "name": "STARRED",
      "messageListVisibility": "hide",
      "labelListVisibility": "labelShow",
      "type": "system"
    },
    {
      "id": "UNREAD",
      "name": "UNREAD",
      "type": "system"
    }
  ]
}