Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php GMail API-如何显示收件箱中的完整电子邮件_Php_Api_Google Api_Gmail Api - Fatal编程技术网

Php GMail API-如何显示收件箱中的完整电子邮件

Php GMail API-如何显示收件箱中的完整电子邮件,php,api,google-api,gmail-api,Php,Api,Google Api,Gmail Api,编辑:修复了第一个错误!现在正在列出邮件ID,仍在尝试如何显示收件箱中的完整电子邮件,如果有人能帮忙的话,将不胜感激 因此,我尝试使用GMail API调用收件箱中的电子邮件,以便在我自己的应用程序中使用它们。我已经对所有访问进行了排序,目前正在返回一个数组。我遇到的问题是试图显示消息 我的php如下所示 <?php ini_set('display_errors' , 'On'); error_reporting(E_ALL); // Start Session session_st

编辑:修复了第一个错误!现在正在列出邮件ID,仍在尝试如何显示收件箱中的完整电子邮件,如果有人能帮忙的话,将不胜感激

因此,我尝试使用GMail API调用收件箱中的电子邮件,以便在我自己的应用程序中使用它们。我已经对所有访问进行了排序,目前正在返回一个数组。我遇到的问题是试图显示消息

我的php如下所示

<?php
ini_set('display_errors' , 'On');
error_reporting(E_ALL);

// Start Session 
session_start();

//Include API autoloader
require_once __DIR__ . '/vendor/autoload.php';

// Create Google Client
$client = new Google_Client();
$client->setClientId('ID in here');
$client->setClientSecret('Secret here');
$client->setRedirectUri('http://localhost:8888/gmail/');
$client->addScope('https://mail.google.com/');

//Create Google Service
$service = new Google_Service_Gmail($client);

if(isset($_REQUEST['logout'])){
    unset($_SESSION['access_token']);
}

// Check if there is an auth token 
if(isset($_GET['code']))
{
    $client->authenticate($_GET['code']);
    $_SESSION['access_token'] = $client->getAccessToken();
    $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    header('Location: ' . filter_var($url, FILTER_VALIDATE_URL));
}

// Check for access token in session 
if(isset($_SESSION['access_token']))
{
    $client->setAccessToken($_SESSION['access_token']);
} else 
{
    $loginUrl = $client->createAuthUrl();
    echo 'Click <a href="' . $loginUrl . '">here</a> to login';
}

// Check if we have an access token ready for API Call
try
{
    if(isset($_SESSION['access_token']) && $client->getAccessToken())
    {
        //make API calls
        $messages = $service->users_messages->listUsersMessages('me',['maxResults'=> 2, 'labelIds'=> 'INBOX']);

        foreach ($messages as $message) 
        {
            print 'Message with ID: ' . $message->getId() . '<br/>';
        }

        return $messages;
    }
}
catch (Google_Auth_Exception $e)
{
    echo 'Looks like you dont have an access token or its expired';
}

添加到@Tholle建议的内容中,
消息列表
的标题将具有
From
属性,以指示消息的发件人。您只需在
foreach messages
中添加一个新循环,或者专门调用
有效负载[x].头。从

中,您必须按所做的那样列出消息,但您还必须对消息内容发出单独的请求。也许会有帮助。