使用 PHP 删除单个 Gmail 联系人

Delete a Single Gmail contact using PHP

我正在使用此脚本从 GMAIL 中删除联系人,有时可以,但不会删除所有联系人示例,我正在尝试删除 "ADRIANA CALI" 此联系人在名为 [=17= 的组中], 但它不会删除这个名字...请问有什么想法吗?

<?php

// load Zend Gdata libraries
//require 'init_autoloader.php';
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');

// set credentials for ClientLogin authentication
$user = "sampleeemail@gmail.com";
$pass = "samplepassword";
//contact name to delete
$contact_name = 'ADRIANA CALI';


try {
   $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, 'cl');
} catch (Zend_Gdata_App_CaptchaRequiredException $cre) {
    echo 'URL of CAPTCHA image: ' . $cre->getCaptchaUrl() . "\n";
    echo 'Token ID: ' . $cre->getCaptchaToken() . "\n";
} catch (Zend_Gdata_App_AuthException $ae) {
   echo 'Problem authenticating: ' . $ae->exception() . "\n";
}
try {       
  // perform login and set protocol version to 3.0
  $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, 'cp');
  $gdata = new Zend_Gdata($client);
  $gdata->setMajorProtocolVersion(3);

  $query = new Zend_Gdata_Query('https://www.google.com/m8/feeds/contacts/default/full');
  $feed = $gdata->getFeed($query);

$contacts=array();;

foreach($feed as $entry){
    $name = (string) $entry->title;
    if(strcasecmp($name,$contact_name)==0) {
        //echo '<xmp>';print_r($entry);echo '</xmp>';
        $id = substr((string) $entry->id,(strpos((string) $entry->id,'base')+5));
        $contacts[] = array('id'=>$id,'name'=>$name); 
        echo $entry->id;
    }
    //$xml = simplexml_load_string($entry->getXML());

    //$obj = new stdClass;echo $entry->title."<br/>";
    //$obj->name = (string) $entry->title;
    //$editLink = $entry->getEditLink()->href;
    //echo $editLink.'-66<br>';
    //$extra_header = array('If-Match'=>'*'); 
    //$entry->delete();
}

  ?>
  <h2>Deleting One Contact</h2>
  <div><?php echo count($contacts); ?> contact(s) found for deletion under Contact "<?php echo $contact_name; ?>".</div>

  <?php


  foreach($contacts as $contact){
      $deleteLink = 'https://www.google.com/m8/feeds/contacts/default/full/'.$contact['id'];

   //$deleteLink = ' http://www.google.com/m8/feeds/contacts/userEmail/base/contactID';

   //echo $deleteLink.'<br>';
   //$deleteLink = $contact['id'];
   //$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, 'cp');
    $client->setHeaders(array('If-Match'=>'*','If-None-Match'=>'anything'));
    $gdata = new Zend_Gdata($client);
    $gdata->setMajorProtocolVersion(3);     
    // delete entry 
    $gdata->delete($deleteLink);


  }
} catch (Exception $e) {
  die('ERROR 71:' . $e->getMessage());  
}
?>

<?php
// display results
foreach ($contacts as $contact) {       
?>
<div class="entry">
  <div class="name"><?php echo (!empty($contact['name'])) ? $contact['name'] : 'Name not available' . ' deleted!'; ?></div>
</div>
<?php
}
?>

这是因为达到了最大联系,查询没有进一步处理,现在我已经将最大限制设置为 1000

<?php

// load Zend Gdata libraries
//require 'init_autoloader.php';
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');

// set credentials for ClientLogin authentication
$user = "sampleemail@gmail.com";
$pass = "sample password";
//contact name to delete
$contact_name = 'ADRIANA CALI';


try {
   $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, 'cl');
} catch (Zend_Gdata_App_CaptchaRequiredException $cre) {
    echo 'URL of CAPTCHA image: ' . $cre->getCaptchaUrl() . "\n";
    echo 'Token ID: ' . $cre->getCaptchaToken() . "\n";
} catch (Zend_Gdata_App_AuthException $ae) {
   echo 'Problem authenticating: ' . $ae->exception() . "\n";
}
try {       
  // perform login and set protocol version to 3.0
  $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, 'cp');
  $gdata = new Zend_Gdata($client);
  $gdata->setMajorProtocolVersion(3);

  $query = new Zend_Gdata_Query('https://www.google.com/m8/feeds/contacts/default/full?max-results=1000');
  $feed = $gdata->getFeed($query);

$contacts=array();;

foreach($feed as $entry){
    $name = (string) $entry->title;
    if(strcasecmp($name,$contact_name)==0) {
        //echo '<xmp>';print_r($entry);echo '</xmp>';
        $id = substr((string) $entry->id,(strpos((string) $entry->id,'base')+5));
        $contacts[] = array('id'=>$id,'name'=>$name); 
        //echo '<br>m-'.$name.'-'.$entry->id;
    }
    //$xml = simplexml_load_string($entry->getXML());

    //$obj = new stdClass;echo $entry->title."<br/>";
    //$obj->name = (string) $entry->title;
    //$editLink = $entry->getEditLink()->href;
    //echo $editLink.'-66<br>';
    //$extra_header = array('If-Match'=>'*'); 
    //$entry->delete();
}

  ?>
  <h2>Deleting One Contact</h2>
  <div><?php echo count($contacts); ?> contact(s) found for deletion under Contact "<?php echo $contact_name; ?>".</div>

  <?php


  foreach($contacts as $contact){
      $deleteLink = 'https://www.google.com/m8/feeds/contacts/default/full/'.$contact['id'];

   //$deleteLink = ' http://www.google.com/m8/feeds/contacts/userEmail/base/contactID';

   //echo $deleteLink.'<br>';
   //$deleteLink = $contact['id'];
   //$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, 'cp');
    $client->setHeaders(array('If-Match'=>'*','If-None-Match'=>'anything'));
    $gdata = new Zend_Gdata($client);
    $gdata->setMajorProtocolVersion(3);     
    // delete entry 
    $gdata->delete($deleteLink);


  }
} catch (Exception $e) {
  die('ERROR 71:' . $e->getMessage());  
}
?>

<?php
// display results
foreach ($contacts as $contact) {       
?>
<div class="entry">
  <div class="name"><?php echo (!empty($contact['name'])) ? $contact['name'] : 'Name not available' . ' deleted!'; ?></div>
</div>
<?php
}
?>