2015-10-17 438 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