patternphpMinor
Gmail contacts API connection
Viewed 0 times
contactsapigmailconnection
Problem
I created this class that connects to Gmail contacts, and enables you to add/edit/delete the contact.
I'm curious to see what others think of my code. If you see any areas for improvement, that would be awesome!
```
$option = $value;
}
// LOGIN TO GMAIL
public function login()
{
if(!isset($this->email)) {
die("Email is not set");
}
if(!isset($this->password)) {
die("Password is not set");
}
$this->client = Zend_Gdata_ClientLogin::getHttpClient($this->email, $this->password, 'cp');
$this->client->setHeaders('If-Match: *');
$this->gdata = new Zend_Gdata($this->client);
$this->gdata->setMajorProtocolVersion($this->protocolVersion);
}
// Get contacts feed
public function getContacts($maxResults = 10)
{
try {
$query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/contacts/default/full');
$query->maxResults = $maxResults;
$query->setParam('orderby', 'lastmodified');
$query->setParam('sortorder', 'descending');
$feed = $this->gdata->getFeed($query);
return $feed;
} catch (Exception $e) {
die('ERROR:' . $e->getMessage());
}
}
// Determine the schema type from Google
// Remove everything but the actual Schema
public function determineSchemaType($value, $format = 'remove')
{
if(!isset($value)) {
die("Schema is not set.");
}
$standard = "http://schemas.google.com/g/2005#";
if($format == 'remove') {
$schema = str_replace($standard, '', $value);
} else if ($format == 'add') {
$schema = $standard . $value;
}
return $schema;
}
// Parse Contacts Feed into simpler object
I'm curious to see what others think of my code. If you see any areas for improvement, that would be awesome!
- The
contact[]is purely for testing.
- I also realize that it could be more robust, but for my needs, this is all that I need.
```
$option = $value;
}
// LOGIN TO GMAIL
public function login()
{
if(!isset($this->email)) {
die("Email is not set");
}
if(!isset($this->password)) {
die("Password is not set");
}
$this->client = Zend_Gdata_ClientLogin::getHttpClient($this->email, $this->password, 'cp');
$this->client->setHeaders('If-Match: *');
$this->gdata = new Zend_Gdata($this->client);
$this->gdata->setMajorProtocolVersion($this->protocolVersion);
}
// Get contacts feed
public function getContacts($maxResults = 10)
{
try {
$query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/contacts/default/full');
$query->maxResults = $maxResults;
$query->setParam('orderby', 'lastmodified');
$query->setParam('sortorder', 'descending');
$feed = $this->gdata->getFeed($query);
return $feed;
} catch (Exception $e) {
die('ERROR:' . $e->getMessage());
}
}
// Determine the schema type from Google
// Remove everything but the actual Schema
public function determineSchemaType($value, $format = 'remove')
{
if(!isset($value)) {
die("Schema is not set.");
}
$standard = "http://schemas.google.com/g/2005#";
if($format == 'remove') {
$schema = str_replace($standard, '', $value);
} else if ($format == 'add') {
$schema = $standard . $value;
}
return $schema;
}
// Parse Contacts Feed into simpler object
Solution
A few things to point out:
You can just
Why are there so many extraneous empty lines?
What's wrong with combining them?
$feed = $this->gdata->getFeed($query);
return $feed;You can just
return feed directly without initialising it.public function determineSchemaType($value, $format = 'remove')
{
if(!isset($value)) {
die("Schema is not set.");
}
$standard = "http://schemas.google.com/g/2005#";
if($format == 'remove') {
$schema = str_replace($standard, '', $value);
} else if ($format == 'add') {
$schema = $standard . $value;
}
return $schema;
}- You should be keeping consistent spacing after your
ifs
- If the
$formatis notremoveoradd,$schemawill return undefined/null
$results[] = $obj;
}
return $results;Why are there so many extraneous empty lines?
$results = $this->gdata->insertEntry($xml, 'http://www.google.com/m8/feeds/contacts/default/full');
echo $results->id;What's wrong with combining them?
echo $this->gdata->insertEntry($xml, 'http://www.google.com/m8/feeds/contacts/default/full')->id;Code Snippets
$feed = $this->gdata->getFeed($query);
return $feed;public function determineSchemaType($value, $format = 'remove')
{
if(!isset($value)) {
die("Schema is not set.");
}
$standard = "http://schemas.google.com/g/2005#";
if($format == 'remove') {
$schema = str_replace($standard, '', $value);
} else if ($format == 'add') {
$schema = $standard . $value;
}
return $schema;
}$results[] = $obj;
}
return $results;$results = $this->gdata->insertEntry($xml, 'http://www.google.com/m8/feeds/contacts/default/full');
echo $results->id;echo $this->gdata->insertEntry($xml, 'http://www.google.com/m8/feeds/contacts/default/full')->id;Context
StackExchange Code Review Q#4404, answer score: 3
Revisions (0)
No revisions yet.