CURL - get Retrieve all email from Google API

1.4k Views Asked by At

I'm using cURL to get all email from user via Google API. Following https://developers.google.com/admin-sdk/email-audit/#retrieving_all_email_monitors_of_a_source_user.

According this tutorial, the server return '201 Created' status code to successful. But, my result return '200 OK' code.

Here is code Authorization

$data = array(
'accountType' => 'HOSTED_OR_GOOGLE',  
'Email' => 'myEmail',  
'Passwd' => 'myPassword',  

'source'=>'PHP-cUrl-Example',  
'service'=>'apps');  
$ch = curl_init();  
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");  
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);   
curl_setopt($ch, CURLOPT_POST, true);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
$response = curl_exec($ch);  

And here is code to Retrieving all email monitors of a source user

preg_match("/Auth=([a-z0-9_-]+)/i", $response, $matches);
$auth = $matches[1];

$header = array('Content-Type: application/atom+xml; charset=utf-8',
            'Authorization: GoogleLogin auth='.trim($auth),
    );
$url_email ="https://apps-apis.google.com/a/feeds/compliance/audit/mail/monitor/mydomain/username";
curl_setopt($ch, CURLOPT_URL, $url_email); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false); 
$response = curl_exec($ch);
$response = simplexml_load_string($response);
curl_close($ch);

print_r($response);

Help me pls ?

1

There are 1 best solutions below

2
Jay Lee On

The API allows you to request the status of a single export request with a URL of:

https://apps-apis.google.com/a/feeds/compliance/audit/mail/export/{domain name}/{source user name}/{mailbox requestId}

or of all requests across the domain with a request of:

https://apps-apis.google.com/a/feeds/compliance/audit/mail/export/{domain name}?fromDate={fromDate}

there is no operation to retrieve the status of all requests for a given user like you are trying to do.

I suggest you confirm you've successfully created an audit request by using GAM to create the request. GAM will show you the request ID on success. Then you can try getting the results of the single request with your code.