2017-08-11 38 views
0

我建立了一个脚本来从Gmail API(我以前的1000封电子邮件)获取数据,我将这个脚本上传到我的网站到一个不同的文件夹只是为了测试,该脚本第一次正常工作,巨大的数据开始加载在页面上,但如果我尝试重新加载页面,它会给出错误504 Gateway Time-out The server didn't respond in time.事实上,我的整个网站停止工作无论我的网站打开我的网站,我得到同样的错误,但如果我关闭浏览器并重新打开它或等待5分钟,然后一切再次开始正常工作。但如果我再次运行脚本,同样的事情再次发生。这是脚本BTW我的网站给出了504错误当我运行特定的脚本

<?php 
session_start(); 
require_once 'google-api-php-client/vendor/autoload.php'; 
$client = new Google_Client; 
$client->setClientId('xxxxxxxxxxxxxxxxxxx'); 
$client->setClientSecret('xxxxxxxxxxxxxxxxxxxxxx'); 
$client->setRedirectUri('http://localhost/gmail_api/redirect.php'); 
$client->addScope(Google_Service_Gmail::GMAIL_READONLY); 
$client->setAccessType('offline');  
//$client->setApprovalPrompt('force'); 
$saved_token = '{"access_token":"xxxxxxxxxxxxxxxxxxxxxx","token_type":"Bearer","expires_in":3600,"refresh_token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxx","created":1502292805}'; 
$access_token = json_decode($saved_token,true); 
if(isset($_SESSION['access_token'])){ 
$access_token = $_SESSION['access_token']; 
} 
$client->setAccessToken($access_token); 

// It still works without this if 
if($client->isAccessTokenExpired()){ 
    $refresh_token = json_decode($saved_token)->refresh_token; 
    $client->refreshToken($refresh_token); 
    $access_token = $client->getAccessToken(); 
    $_SESSION['access_token']= $access_token; 
} 

?> 
<!doctype html> 
<html> 
<head> 
<style type="text/css"> 
body{ 
    background: white !important; 
    text-align:center; 
} 
</style> 

</head> 
<body> 
<form method="get" action=""> 
<input type="text" name="search"> 
<input type="submit" value="search"> 
</form> 
<?php 
function decodeBody($body) { 
    $rawData = $body; 
    $sanitizedData = strtr($rawData,'-_', '+/'); 
    $decodedMessage = base64_decode($sanitizedData); 
    if(!$decodedMessage){ 
     $decodedMessage = FALSE; 
    } 
    return $decodedMessage; 
} 
function getHeader($headers, $name) { 
     foreach($headers as $header) { 
    if($header['name'] == $name) { 
     return $header['value']; 
     } 
    } 
    } 


try{  
     $gmail = new Google_Service_Gmail($client); 
     $optParams = []; 
       $optParams['maxResults'] = 1000; // Return Only 1000 Messages 
       $optParams['labelIds'] = 'INBOX'; // Only show messages in Inbox 
       $optParams['includeSpamTrash'] = false; //No Messages from spam or trash 
       $optParams['q'] = $_GET['search']; 
       $list = $gmail->users_messages->listUsersMessages('me',$optParams); 

    while ($list->getMessages() != null) { 

     foreach ($list->getMessages() as $mlist) { 

      $message_id = $mlist->id; 
      $optParamsGet2['format'] = 'full'; 
      $single_message = $gmail->users_messages->get('me', $message_id, $optParamsGet2); 
      $headers = $single_message->getPayload()->getHeaders(); 
      $payload = $single_message->getPayload(); 

      // With no attachment, the payload might be directly in the body, encoded. 
      $body = $payload->getBody(); 
      $FOUND_BODY = decodeBody($body['data']); 

      // If we didn't find a body, let's look for the parts 
      if(!$FOUND_BODY) { 
       $parts = $payload->getParts(); 
       foreach ($parts as $part) { 
        if($part['body'] && $part['mimeType'] == 'text/html') { 
         $FOUND_BODY = decodeBody($part['body']->data); 
         break; 
        } 
       } 
      } if(!$FOUND_BODY) { 
       foreach ($parts as $part) { 
        // Last try: if we didn't find the body in the first parts, 
        // let's loop into the parts of the parts (as @Tholle suggested). 
        if($part['parts'] && !$FOUND_BODY) { 
         foreach ($part['parts'] as $p) { 
          // replace 'text/html' by 'text/plain' if you prefer 
          if($p['mimeType'] === 'text/html' && $p['body']) { 
           $FOUND_BODY = decodeBody($p['body']->data); 
           break; 
          } 
         } 
        } 
        if($FOUND_BODY) { 
         break; 
        } 
       } 
      } 

       $subject = getHeader($headers,'Subject'); 
       $Date = getHeader($headers,'Date'); 
       $From = getHeader($headers,'From'); 
      // Finally, print the message ID and the body 
       echo "<h2>$subject </h2>"; 
      echo "Date:$Date <br>"; 
      echo "From:". htmlspecialchars($From)."<br>"; 
      print_r($FOUND_BODY); 
     } 

     if ($list->getNextPageToken() != null) { 
      $pageToken = $list->getNextPageToken(); 
      $list = $gmail->users_messages->listUsersMessages('me', array('pageToken' => $pageToken)); 
     } else { 
      break; 
     } 
    } 
} catch (Exception $e) { 
    echo $e->getMessage(); 
    unset($_SESSION['access_token']); 
} 

?> 
</body> 
</html> 

它在本地主机上的伟大工程,无所谓多少次我重新加载页面

任何猜测可能是错的?

回答

相关问题