php 谷歌地图api 实例,php 实现google contact api 实例

/*

* google contact api php

* @author qintao

* @2012-12-01

*/

require_once 'Zend/Loader.php';

Zend_Loader::loadClass ( 'Zend_Gdata' );

Zend_Loader::loadClass ( 'Zend_Gdata_ClientLogin' );

Zend_Loader::loadClass ( 'Zend_Http_Client' );

Zend_Loader::loadClass ( 'Zend_Gdata_Query' );

Zend_Loader::loadClass ( 'Zend_Gdata_Feed' );

class contact {

private $client;

public $gdata;

public $doc;

public $url_delete = '%user/base/%id';

public $url_search = '%user/full/%id';

public $url_edit = 'default/full/%id';

public $user;

public $password;

public $domain = '@gmail';

function __construct($user, $password) {

// perform login and set protocol version to 3.0

try {

$this->user = $user;

$this->password = $password;

$this->client = Zend_Gdata_ClientLogin::getHttpClient ( $this->user, $this->password, 'cp' );

$this->gdata = new Zend_Gdata ( $this->client );

$this->gdata->setMajorProtocolVersion ( 3 );

} catch ( Exception $e ) {

die ( 'ERROR:' . $e->getMessage () );

}

}

public function addContact(array $array) {

try {

$this->doc = new DOMDocument ();

$this->doc->formatOutput = true;

$entry = $this->doc->createElement ( 'atom:entry' );

$entry->setAttributeNS ( '', 'xmlns:atom', '' );

$entry->setAttributeNS ( '', 'xmlns:gd', '' );

$entry->setAttributeNS ( '', 'xmlns:batch', '' );

$this->doc->appendChild ( $entry );

// add name element

$name = $this->doc->createElement ( 'gd:name' );

$entry->appendChild ( $name );

$firstName = $this->doc->createElement ( 'gd:firstName', $array ['firstName'] );

$name->appendChild ( $firstName );

$lastName = $this->doc->createElement ( 'gd:middleName', $array ['middleName'] );

$name->appendChild ( $lastName );

$middleName = $this->doc->createElement ( 'gd:lastName', $array ['lastName'] );

$name->appendChild ( $middleName );

$fullName = $this->doc->createElement ( 'gd:fullName', $array ['lastName'] . $array ['middleName'] . $array ['firstName'] );

$name->appendChild ( $fullName );

// add email element

for($i = 0; $i < count ( $array ['email'] ); $i ++) {

$email = $this->doc->createElement ( "gd:email" );

$email->setAttribute ( 'address', $array ["email"] [$i] );

$email->setAttribute ( 'rel', '#' . self::getType ( $i ) );

$entry->appendChild ( $email );

}

for($i = 0; $i < count ( $array ['phoneNumber'] ); $i ++) {

$phoneNumber = $this->doc->createElement ( "gd:phoneNumber", $array ['phoneNumber'] [$i] );

$phoneNumber->setAttribute ( 'rel', '#' . self::getType ( $i ) );

$entry->appendChild ( $phoneNumber );

}

//var_dump($this->doc->saveXML ());

// insert entry

$entryResult = $this->gdata->insertEntry ( $this->doc->saveXML (), 'default/full' );

return $entryResult->id;

} catch ( Exception $e ) {

die ( 'ERROR:' . $e->getMessage () );

}

}

public function deleteContact($id) {

try {

$id = str_replace ( array ('%user', '%id' ), array ($this->user . $this->domain, $id ), $this->url_delete );

$this->client->setHeaders ( 'If-Match: *' );

$this->gdata = new Zend_Gdata ( $this->client );

$this->gdata->setMajorProtocolVersion ( 3 );

// delete entry

$this->gdata->delete ( $id );

} catch ( Exception $e ) {

die ( 'ERROR:' . $e->getMessage () );

}

}

public function getContact($id) {

try {

// perform query and get feed of all results

$id = str_replace ( array ('%user', '%id' ), array ($this->user . $this->domain . ".com", $id ), $this->url_search );

$feed = $this->gdata->getEntry ( $id );

$xml = simplexml_load_string ( $feed->getXML () );

$results = array ();

$obj = new stdClass ();

$obj->edit = $feed->getEditLink ()->href;

$xml = simplexml_load_string ( $feed->getXML () );

$obj->name = ( string ) $feed->title;

$obj->orgName = ( string ) $xml->organization->orgName;

$obj->orgTitle = ( string ) $xml->organization->orgTitle;

foreach ( $xml->email as $e ) {

$obj->emailAddress [] = ( string ) $e ['address'];

}

foreach ( $xml->phoneNumber as $p ) {

$obj->phoneNumber [] = ( string ) $p;

}

foreach ( $xml->website as $w ) {

$obj->website [] = ( string ) $w ['href'];

}

foreach ( $xml->structuredPostalAddress as $s ) {

$obj->formattedAddress [] = ( string ) $s->formattedAddress;

}

$results [] = $obj;

print_r ( $results );

} catch ( Exception $e ) {

die ( 'ERROR:' . $e->getMessage () );

}

}

public function getContactAll() {

try {

// perform query and get feed of all results

$query = new Zend_Gdata_Query ( 'default/full' );

$query->maxResults = 0;

$query->setParam ( 'orderby', 'lastmodified' );

$query->setParam ( 'sortorder', 'descending' );

$feed = $this->gdata->getFeed ( $query );

$results = array ();

foreach ( $feed as $entry ) {

$obj = new stdClass ();

$obj->edit = $entry->getEditLink ()->href;

$obj->id = end(explode("/", $entry->edit));

$xml = simplexml_load_string ( $entry->getXML () );

$obj->name = ( string ) $entry->title;

$obj->edited = (string)$xml->edited;

$obj->orgName = ( string ) $xml->organization->orgName;

$obj->orgTitle = ( string ) $xml->organization->orgTitle;

foreach ( $xml->email as $e ) {

$obj->emailAddress [] = ( string ) $e ['address'];

}

foreach ( $xml->phoneNumber as $p ) {

$obj->phoneNumber [] = ( string ) $p;

}

foreach ( $xml->website as $w ) {

$obj->website [] = ( string ) $w ['href'];

}

$results [] = $obj;

}

//print_r($results);

return $results;

} catch ( Exception $e ) {

die ( 'ERROR:' . $e->getMessage () );

}

}

public function editContact($id, array $array) {

try {

$this->client->setHeaders ( 'If-Match: *' );

$this->gdata->setMajorProtocolVersion ( 3 );

// perform query and get entry

$id = str_replace('%id', $id, $this->url_edit);

$query = new Zend_Gdata_Query ( $id );

$entry = $this->gdata->getEntry ( $query );

$xml = simplexml_load_string ( $entry->getXML () );

//edit

if(!empty($array))

{

foreach ($array as $key=>$val)

{

if($key == 'name'){

$this->nameEdit(&$xml, $val);

}elseif($key == 'phoneNumber')

{

$this->phoneNumberEdit(&$xml, $val);

}elseif($key == 'email'){

$this->emailEdit(&$xml, $val);

}elseif($key == 'address')

{

$this->addressEdit(&$xml, $val);

}

}

}

$entryResult = $this->gdata->updateEntry($xml->saveXML(), $entry->getEditLink()->href);

} catch ( Exception $e ) {

die ( 'ERROR:' . $e->getMessage () );

}

}

public function nameEdit($xml,$val)

{

if(count($val) > 0)

{

if(isset($val['firstName']) && !empty($val['firstName']))

{

$xml->name->familyName = $val['firstName'];

}

if(isset($val['lastName']) && !empty($val['lastName'])){

$xml->name->givenName = $val['lastName'];

}

if(isset($val['middleName'])) $xml->name->additionalName = $val['middleName'];

$xml->name->fullName = $val['firstName'].$val['middleName'].$val['lastName'];

}else{

return;

}

}

public function phoneNumberEdit($xml,$val)

{

if(count($val) > 0)

{

foreach ($val as $k=>$v)

{

if(isset($val[$k]) && !empty($val[$k])) $xml->phoneNumber[$k] = $val[$k];

}

}else{

return;

}

}

public function emailEdit($xml,$val)

{

if(count($val) > 0)

{

foreach ($val as $k=>$v)

{

if(isset($val[$k]) && !empty($val[$k])) $xml->email[$k]['address'] = $val[$k];

}

}else{

return;

}

}

public function addressEdit($xml,$val)

{

if(count($val) > 0)

{

foreach ($val as $k=>$v)

{

if(isset($val[$k]) && !empty($val[$k])) $xml->structuredPostalAddress[$k]->formattedAddress = $val[$k];

}

}else{

return;

}

}

public static function getType($i, $application = 'email') {

if ($application == 'email' && $i > 1)

$i = 10;

switch ($i) {

case 0 :

$type = 'home';

break;

case 1 :

$type = 'work';

break;

case 2 :

$type = 'mobile';

break;

default :

$type = 'other';

break;

}

empty ( $type ) && $type = 'other';

return $type;

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值