2015-10-17 290 views
2

在女巫模式下可以用php api读取邮件主题?我怎样才能读取一个与PHP API的Gmail主题?

我做这个代码

// 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的,因为没有关于功能的任何文件。 我可以找到补充文件? (例如我发现getPayload功能,因为我在这里看到了一些例子)

感谢

回答

7

如果你看一下访问与常规HTTP的请求的API,当你得到一个光秃秃的骨头的反应,我想了很多的事情会更清楚。

我会list我最新的消息,然后get的消息,看看响应:

请求

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": "[email protected]" 
    }, 
    { 
    "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": "<[email protected]outube-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 <[email protected]>  (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 [email protected]outube-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 [email protected]outube-subscriptions.bounces.google.com designates 2607:f8b0:4003:c01::245 as permitted sender) smtp.mailfrom[email protected]youtube-subscriptions.bounces.google.com;  dkim=pass [email protected];  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 <[email protected]>; 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": "<[email protected]outube-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 <[email protected]>  (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 [email protected]outube-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 <[email protected]>; 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": "<[email protected]>" 
    }, 
    { 
    "name": "Date", 
    "value": "Sun, 18 Oct 2015 04:01:21 +0000" 
    }, 
    { 
    "name": "Subject", 
    "value": "Infinite Elgintensity just uploaded a video" 
    }, 
    { 
    "name": "From", 
    "value": "YouTube <[email protected]>" 
    }, 
    { 
    "name": "To", 
    "value": "[email protected]" 
    }, 
    { 
    "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将位于​​的内部,它是您要查找的Subject-标题。

没有为得到一个特定的头部没有的功能,但它并不难实现:

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'); 
} 
+1

非常感谢你为你完整的答案!!!! – colapiombo

+1

@ BrancaMenta1845没问题:)很高兴我能帮忙! – Tholle

+1

$ header-> name和$ header-> value对我来说是$ header ['name']和$ header ['value'] – colapiombo