如何使用php API阅读Gmail主题? [英] how can i read a gmail subject with php api?

查看:132
本文介绍了如何使用php API阅读Gmail主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在女巫模式下,可以使用php api阅读电子邮件主题吗?

in witch mode a can read the email subject with php api ?

我执行此代码

// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Gmail($client);

// Print the labels in the user's account.
$user = 'me';


$results = $service->users_messages->listUsersMessages($user);
foreach($results as $mail){
  $optParamsGet['format'] = 'metadata'; // Display message in payload
  $message = $service->users_messages->get($user, $mail['id'],$optParamsGet);
  $objMessage = $message->getPayload();
  print_r($objMessage[18]['value']);
}

我发现很难将此api与php一起使用,因为没有有关该函数的任何文档. 在哪里可以找到comple文档? (例如,我找到了getPayload函数,因为我在这里看到了一些示例)

i find very difficult use this api with php because there isn't any documentation about the function. where i can find the comple documentation? (for example i found getPayload function because i see some example here)

谢谢

推荐答案

如果您看一下使用常规http请求访问API时获得的基本响应,我认为很多事情会更加清楚.

If you have a look at a bare bones response you get when accessing the API with regular http-requests, I think a lot of things will be more clear.

我将列表我的最新消息,以及然后获取消息,然后查看回应:

I will list my latest message, and then get the message, and have a look at the response:

请求

maxResults = 1

GET https://www.googleapis.com/gmail/v1/users/me/messages?maxResults=1

回复

{
 "messages": [
  {
   "id": "150791be31ee4e7b",
   "threadId": "150791be31ee4e7b"
  }
 ],
 "nextPageToken": "05858850367051193217",
 "resultSizeEstimate": 2
}

请求

GET https://www.googleapis.com/gmail/v1/users/me/messages/150791be31ee4e7b

回复

{
 "id": "150791be31ee4e7b",
 "threadId": "150791be31ee4e7b",
 "labelIds": [
  "INBOX",
  "CATEGORY_SOCIAL",
  "UNREAD"
 ],
 "snippet": "Infinite Elgintensity has uploaded Gym Idiots - The Smith Machine's Retarded Cousin I have no",
 "historyId": "623043",
 "internalDate": "1445140881000",
 "payload": {
  "mimeType": "multipart/alternative",
  "filename": "",
  "headers": [
   {
    "name": "Delivered-To",
    "value": "emtholin@gmail.com"
   },
   {
    "name": "Received",
    "value": "by 10.28.188.5 with SMTP id m5csp628249wmf;        Sat, 17 Oct 2015 21:01:22 -0700 (PDT)"
   },
   {
    "name": "X-Received",
    "value": "by 10.182.28.74 with SMTP id z10mr15179312obg.80.1445140882358;        Sat, 17 Oct 2015 21:01:22 -0700 (PDT)"
   },
   {
    "name": "Return-Path",
    "value": "<3kRkjVgcLDAgvwzmxt66w212jm.kwu1p2ttmB-898Fxiom0.xt20owwotm.kwu@youtube-subscriptions.bounces.google.com>"
   },
   {
    "name": "Received",
    "value": "from mail-ob0-x245.google.com (mail-ob0-x245.google.com. [2607:f8b0:4003:c01::245])        by mx.google.com with ESMTPS id l8si12292524oej.1.2015.10.17.21.01.22        for <emtholin@gmail.com>        (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);        Sat, 17 Oct 2015 21:01:22 -0700 (PDT)"
   },
   {
    "name": "Received-SPF",
    "value": "pass (google.com: domain of 3kRkjVgcLDAgvwzmxt66w212jm.kwu1p2ttmB-898Fxiom0.xt20owwotm.kwu@youtube-subscriptions.bounces.google.com designates 2607:f8b0:4003:c01::245 as permitted sender) client-ip=2607:f8b0:4003:c01::245;"
   },
   {
    "name": "Authentication-Results",
    "value": "mx.google.com;       spf=pass (google.com: domain of 3kRkjVgcLDAgvwzmxt66w212jm.kwu1p2ttmB-898Fxiom0.xt20owwotm.kwu@youtube-subscriptions.bounces.google.com designates 2607:f8b0:4003:c01::245 as permitted sender) smtp.mailfrom=3kRkjVgcLDAgvwzmxt66w212jm.kwu1p2ttmB-898Fxiom0.xt20owwotm.kwu@youtube-subscriptions.bounces.google.com;       dkim=pass header.i=@youtube.com;       dmarc=pass (p=QUARANTINE dis=NONE) header.from=youtube.com"
   },
   {
    "name": "Received",
    "value": "by mail-ob0-x245.google.com with SMTP id da8so130023858obb.1        for <emtholin@gmail.com>; Sat, 17 Oct 2015 21:01:22 -0700 (PDT)"
   },
   {
    "name": "X-Received",
    "value": "by 10.107.148.204 with SMTP id w195mr21311032iod.15.1445140881929;        Sat, 17 Oct 2015 21:01:21 -0700 (PDT)"
   },
   {
    "name": "X-Received",
    "value": "by 10.140.238.83 with SMTP id j80mr20061780qhc.2.1445140881732;        Sat, 17 Oct 2015 21:01:21 -0700 (PDT)"
   },
   {
    "name": "Return-Path",
    "value": "<3kRkjVgcLDAgvwzmxt66w212jm.kwu1p2ttmB-898Fxiom0.xt20owwotm.kwu@youtube-subscriptions.bounces.google.com>"
   },
   {
    "name": "Received",
    "value": "from mail-pa0-f72.google.com (mail-pa0-f72.google.com. [209.85.220.72])        by gmr-mx.google.com with ESMTPS id wl2si2608966pab.1.2015.10.17.21.01.21        for <thulle3-0107@pages.plusgoogle.com>        (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);        Sat, 17 Oct 2015 21:01:21 -0700 (PDT)"
   },
   {
    "name": "Received-SPF",
    "value": "pass (google.com: domain of 3kRkjVgcLDAgvwzmxt66w212jm.kwu1p2ttmB-898Fxiom0.xt20owwotm.kwu@youtube-subscriptions.bounces.google.com designates 209.85.220.72 as permitted sender) client-ip=209.85.220.72;"
   },
   {
    "name": "Received",
    "value": "by mail-pa0-f72.google.com with SMTP id da3so25pad.2        for <thulle3-0107@pages.plusgoogle.com>; Sat, 17 Oct 2015 21:01:21 -0700 (PDT)"
   },
   {
    "name": "DKIM-Signature",
    "value": "v=1; a=rsa-sha256; c=relaxed/relaxed;        d=youtube.com; s=20120806;        h=mime-version:message-id:date:subject:from:to:content-type;        bh=dxXkvZ79qLn59RVfXz4QAs5AW2+MyxrsrN55S+jKtK0=;        b=mrrriloHJkEy4TT3wpzhEO1nvinpewLWHHExGGprhWlUU+5ME9M8w3xlaTeDcm0V0y         QYDbH6TTG2rc0vwiLuZ46Dgm3rDC0brbg/Bdh3TQRUBneOTdAW4oMMs48DSIBbbFa58j         U7GHaaXGNtsY3ah4D2x8Bv9XxPbtN1YoMEV+DFlYAStrIyyUiGXk7yotcBWqbB89/MFt         ga9f+5Rhf38/TggWLRD88fBp8u2EIDB5VwqHsX5IstX2+Y0yb+yf4sk9iiToAnIXZhiC         Ngqp3YdgLrI6+UlAvixbdB42nlxsGbBJ9WoF4dejLEIeQps47loxNuA2hBsiO1wqSE6n         3XYA=="
   },
   {
    "name": "MIME-Version",
    "value": "1.0"
   },
   {
    "name": "X-Received",
    "value": "by 10.66.182.162 with SMTP id ef2mr20318738pac.35.1445140881616; Sat, 17 Oct 2015 21:01:21 -0700 (PDT)"
   },
   {
    "name": "X-No-Auto-Attachment",
    "value": "1"
   },
   {
    "name": "Message-ID",
    "value": "<047d7bd6aa64e62f22052259149a@google.com>"
   },
   {
    "name": "Date",
    "value": "Sun, 18 Oct 2015 04:01:21 +0000"
   },
   {
    "name": "Subject",
    "value": "Infinite Elgintensity just uploaded a video"
   },
   {
    "name": "From",
    "value": "YouTube <noreply@youtube.com>"
   },
   {
    "name": "To",
    "value": "thulle3-0107@pages.plusgoogle.com"
   },
   {
    "name": "Content-Type",
    "value": "multipart/alternative; boundary=047d7bd6aa64e62f000522591497"
   }
  ],
  "body": {
   "size": 0
  },
  "parts": [
   {
    "partId": "0",
    "mimeType": "text/plain",
    "filename": "",
    "headers": [
     {
      "name": "Content-Type",
      "value": "text/plain; charset=UTF-8; format=flowed; delsp=yes"
     }
    ],
    "body": {
     "size": 301,
     "data": "SW5maW5pdGUgRWx..."
    }
   },
   {
    "partId": "1",
    "mimeType": "text/html",
    "filename": "",
    "headers": [
     {
      "name": "Content-Type",
      "value": "text/html; charset=UTF-8"
     },
     {
      "name": "Content-Transfer-Encoding",
      "value": "quoted-printable"
     }
    ],
    "body": {
     "size": 16884,
     "data": "DQo8aHRtbCBsYW5nPSJl..."
    }
   }
  ]
 },
 "sizeEstimate": 22314
}

这是典型消息的外观,各个库所做的只是使它更易于使用. headers将驻留在payload的内部,它是您要查找的Subject标题.

This is how a typical message will look, and all that the various libraries does is to make it easier to work with. The headers will reside inside of the payload, and it's the Subject-header you are looking for.

没有获取特定标头的功能,但实现起来并不难:

There is no function for getting a specific header, but it's not that hard to implement:

function getHeader($headers, $name) {
  foreach($headers as $header) {
    if($header['name'] == $name) {
      return $header['value'];
    }
  }
}

$results = $service->users_messages->listUsersMessages($user);
foreach($results as $mail){
  $message = $service->users_messages->get($user, $mail['id']);
  $headers = $message->getPayload()->getHeaders();
  $subject = getHeader($headers, 'Subject');
}

这篇关于如何使用php API阅读Gmail主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆