Gmail OAuth API Count Messages PHP - php

I have the following code to count unread emails with Gmail OAuth API in PHP. The problem I have it will only return a max 100 due to API rate limits. Is there anyway to count unread emails using oauth (not imap) and return correct value even if more than 100 items?
$optParams['labelIds'] = 'UNREAD';
$messages = $service->users_messages->listUsersMessages('me',$optParams);
$list = $messages->getMessages();
$result = count($list);
echo "Total Inbox = ".$result ;

There is an estimatedResultCount that's always returned on all list() calls but it's an estimate clearly.
If you want an exact count of total+unread messages on a label then you can now get that from label.get().

The way we were able to solve it is by creating a condition that if the number of unread emails exceeds 100 we simply put it in the following way "+100".

Related

GMail API - Getting certain message headers or fields

I have successfully connected GMail API via G Suite account and service account. I can get a message list and I can retrieve messages by IDs. I'm working with PHP.
What I'm having problems with is to get for example the FROM or TO headers, SUBJECT or the snippet field.
$optParam = array('format' => 'metadata', 'metadataHeaders'=>['subject','from'], 'fields'=>['snippet','labelIds']);
$fullMessage = $service->users_messages->get($user, $id, $optParam);
This will return the snippet, but not the subject or from or the labelIds.
If I use the GMail "Try this API" and use the id of the message and use "snippet" in the "fields" entry, I just get the snippet back as:
{
"snippet": "Short snippet of the message"
}
If I use:
$optParam = array('format' => 'metadata', 'metadataHeaders'=>['subject','from','to']);
I do get the 3 headers, but I also get a lot more information, including the labels and snippet - about 3K for each message.
I just can't seem to be able to specify a small subset of the data. All I need is to show messages as a list with the subject, date/time, from/to.
I don't care so much about the amount of data, but it takes on average about 3.5 seconds to retrieve the data for just 14 message!
Is there a way to restrict this so I don't get all the "extra" data or speed the retrieval up somehow?
Sending the request would involve specifying the metadata keys as well as the parameter names of the fields you want to obtain. You can use an HTTP GET request with the URI to get the ‘to’, ‘from’, ‘subject’ and ‘snippet’ with https://www.googleapis.com/gmail/v1/users/me/messages/<MESSAGE_ID>?format=metadata&metadataHeaders=to&metadataHeaders=from&metadataHeaders=subject&fields=snippet%2C+payload%2Fheaders, which will also limit the headers you obtain.
In PHP you can use this:
$optParam = array('format' => 'metadata', 'metadataHeaders'=>['subject', 'from', 'to'], 'fields'=>'payload/headers,snippet');
Note that the fields parameter needs to be sent as a string and not an array.
Also be aware there is a known issue with the Gmail API where using the https://www.googleapis.com/auth/gmail.metadata scope will not return the snippet.
You’ll need to use https://www.googleapis.com/auth/gmail.readonly instead.
You can also make a batch of requests in one network call which will help speed up overall execution time as documented here.

Docusign rate limit to receive documents in 1 envelope

I am trying to switch to the production API of Docusign. When I submit the required 20 envelopes for approval they do not get approved. I recieved a log file that lists multiple GET requests. It violates the API rules, only one GET request per envelope per 15 minutes is allowed according to the documentation. (https://developers.docusign.com/esign-rest-api/guides/resource-limits)
When I list my envelope and loop through the envelope multiple times to get the documents out of it. I do multiple GET requests to the same envelope and that's why I think I get a rate limit error.
In the example below, you can see that when I retrieve the envelope, I immediately loop over the documents inside the envelope and get the documents with the getDucument method as described in the documentation. (https://developers.docusign.com/esign-rest-api/code-examples/get-an-envelope-document-list)
public function getEnvelopeDocument ($envelopeId)
{
$documents = $this->envelopeApi->listDocuments(config('docusign.id'), $envelopeId);
try {
foreach($documents->getEnvelopeDocuments() as $document)
{
$docs[] = $this->envelopeApi->getDocument((config('docusign.id')), $document->getDocumentId(), $envelopeId);
}
} catch (ApiException $e){
dd("Error connecting Docusign : " . $e->getResponseBody()->errorCode . " " . $e->getResponseBody()->message);
}
}
Am I violating the API rate limiter? If so, what would be the allowed way to retrieve documents inside an envelope.
My interpretation of the code is that you are performing the following calls in sequence:
GET /envelopes/{envelopeId}/documents - ListDocuments
GET /envelopes/{envelopeId}/documents/1 - get document 1
GET /envelopes/{envelopeId}/documents/2 - get document 2
and so on.
If this is the case, you are not in violation of the API limit. If you were to make two calls to the ListDocuments or to one of the individual documents within 15 minutes that would be a polling violation.
To confirm everything is acceptable, you might capture API logs to confirm you're hitting each unique endpoint only once. Info on API logs is available here: https://support.docusign.com/guides/ndse-user-guide-api-request-logging

Hubspot bulk add property

I want to add multiple emails at once through Hubspot API. I used a foreach loop to add emails but, after 30 seconds, it is showing execution time is exceeded. Is there a bulk way to add a property through Hubspot API?
foreach ($leads as $lead) {
$data['email'] = $lead['email'];
$data['firstname'] = $lead['name'];
$hubspot = new HubSpot($autoDetails[3]->api_creds_required);
$hubspot->contacts()->create_contact($data);
$email = $hubspot->contacts()->get_contact_by_email($lead['email']);
$result = $hubspot->lists()->add_contacts_to_list($email->vid, $autoDetails[0]);
}
As recommended in Hubspot's docs, you'll want to use their Batch API, which allows you to create or update up to 100 contacts in one request.
You might also want to check out this open source PHP Hubspot wrapper, which supports this same endpoint.

Calculate address from latitude and longitude in google API (More than one address calculation in one request)

This function for Reveres Geocoding works well for me. I have to display the address in the information window of a marker, and there are more then 200 in one map. I built the code below and it works perfectly. However, Google's API request limit of 2500/day gets exceeded easily.
Is there any way in I can bulk request Google's API?
Our project is currently a small one, so buying more requests/day isn't an option for us.
<?php
function getaddress($lat,$lng)
{
$url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat).','.trim($lng).'&sensor=false';
$json = #file_get_contents($url);
$data=json_decode($json);
$status = $data->status;
if($status=="OK")
return $data->results[0]->formatted_address;
else
return false;
}
?>
No, you can't "bulk query" a bunch of addresses and have it only count as one request when using the standard API. Each lookup counts as a request.
There are other APIs that don't have limits, or allow more lookups, like MapQuest. If you're hitting the daily limit with Google, try using another API.

Gmail oauth received on date using PHP

How do I go about getting a count of emails received on a particular date across all gmail folders. Managed to get email inbox counting working ok but now need to figure out how to get number received for previous day using PHP and oauth. Can anyone help?
The messages#list method allows you to pass a q parameter which accepts the same query format from the Gmail search field.
Thus it's possible to do this:
// $gmail is an instance of Google_Service_Gmail
$messages = $gmail->users_messages->listUsersMessages('me', array(
'q' => "after:2014/10/20 before:2014/10/21"
));
// $count is the number of messages from 2014/10/20
$count = $messages->getResultSizeEstimate();
Beware that the result will be different from what you see in Gmail if you have the 'Conversation view' turned on.

Categories