我使用gmail api,當我試圖從特定消息中獲取「From」字段時,我得到的是名稱部分,但不是地址。下面是在的oauth2操場工程查詢:Gmail api響應標題 - 獲取尖括號中所包含的信息(php庫)
它輸出:
{
"payload": {
"mimeType": "multipart/mixed",
"headers": [
{
"name": "From",
"value": "First Last <[email protected]>"
}
]
},
"sizeEstimate": 180758
}
實現在PHP中相同的查詢:
function getMessage($service, $userId, $messageId) {
try {
$optParamsGet = array();
$optParamsGet['format'] = 'metadata';
$optParamsGet['metadataHeaders'] = 'From';
$optParamsGet['fields'] = 'payload,sizeEstimate';
$message = $service->users_messages->get($userId, $messageId, $optParamsGet);
print 'Message with ID: ' . $message->getId() . ' retrieved.<br>';
return $message;
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
}
}
$message2 = getMessage($service,$userId,$message_id);
if ($message2->getPayload()) {
$headers = $message2->getPayload()->getHeaders();
foreach ($headers as $header) {
if ('From' == $header->name) {
$name_value = $header->value;
echo $name_value . "<br>";
array_push($sendernames,$name_value);
echo $header['value']. "<br>";
break;
}
}
}
var_dump($headers);
而現在,做一個返回的$消息中頭部的var_dump不顯示尖括號內的電子郵件地址。我如何得到這個?
頭如果你'將值直接回顯到一個HTML文檔,你會想通過'htmlspecialchars()'來管它,例如'echo htmlspecialchars($ name_value)' – Phil 2014-10-20 02:39:55
是的!謝謝。 – user3200528 2014-10-20 02:53:54