title: What's wrong with this gmail contact importer script for Drupal?
body: <p>Drupal has a gmail contact importer module that's been oh-so-close to working for about 7 months now.  I don't have the chops to debug the problem, the maintainer vanished, and I think this is an important piece to have working.  So I turn to StackOverflow for help.</p>
  
  <p>The Problem: When you fill in your credentials and tell the script to bring back your contacts, by default it pulls in 25.  That works fine.</p>
  
  <p>When you tell it to look for more contacts by changing the URL you're querying from this:</p>
  
  <pre><code>http://www.google.com/m8/feeds/contacts/default/thin  
  </code></pre>
  
  <p>to this: </p>
  
  <pre><code>http://www.google.com/m8/feeds/contacts/default/thin?max-results=1000
  </code></pre>
  
  <p>You get the following fatal error:</p>
  
  <blockquote>
    <p>Fatal error: Call to a member function <code>getAttribute()</code> on a non-object in <code>path/to/site/sites/all/modules/dcl_importer/scripts/importGData.class.php</code> on line 97 </p>
  </blockquote>
  
  <p>Here's the script: </p>
  
  <pre><code>class GDataMailer {
      static $url_ClientLogin = 'https://www.google.com/accounts/ClientLogin';
      static $url_Feed = 'http://www.google.com/m8/feeds/contacts/default/thin?max-results=65535';
  
      function newCurlSession($URL, $auth = null) {
          $curl = curl_init();
  
          $opts = array(
              CURLOPT_URL =&gt; $URL,
              CURLOPT_REFERER =&gt; '',
              CURLOPT_SSL_VERIFYPEER =&gt; false,
              CURLOPT_SSL_VERIFYHOST =&gt; true,
              CURLOPT_RETURNTRANSFER =&gt; true,
              CURLOPT_FOLLOWLOCATION =&gt; true,
          );
          if (null != $auth) {
              $opts[CURLOPT_HTTPHEADER] = array(
                 'Authorization: GoogleLogin auth='.$auth,
              );
          }
          curl_setopt_array($curl, $opts);
          return $curl;
      }
  
      function useCurlForPost($curl, $params) {
          curl_setopt($curl, CURLOPT_POST, true);
          curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
          return $curl;
      }
  
      function getAuthToken($login, $password) {
          $curl = $this-&gt;useCurlForPost($this-&gt;newCurlSession(self::$url_ClientLogin), array(
             'accountType' =&gt; 'HOSTED_OR_GOOGLE',
             'Email' =&gt; $login,
             'Passwd' =&gt; $password,
             'service' =&gt; 'cp',
             'source' =&gt; 'Drupal-Contact-Importer',
          ));
          $resp = curl_exec($curl);
  
          // Look for the important stuff:
          preg_match_all('/Auth=([^\s]*)/si', $resp, $matches);
          if (isset($matches[1][0])) {
              return $matches[1][0];
          } else {
             return false;
          }
      }
  
      function getAddressbook($login, $password) {
          // check if username and password was given:
          if ((isset($login) &amp;&amp; trim($login)=="") || (isset($password) &amp;&amp; trim($password)==""))
          {
              $args = func_get_args();
              drupal_set_message('Incorrect login information given: '.print_r($args, true), 'error');
              return false;
          }
  
          // Get the GData auth token:
          $auth = $this-&gt;getAuthToken($login, $password);
          if (false === $auth) {
              drupal_set_message('Login failed', 'error');
              return false;
          }        
  
          $curl = $this-&gt;newCurlSession(self::$url_Feed, $auth);
          $data = curl_exec($curl);
  
          $doc = new DOMDocument();
          $doc-&gt;loadXML($data);
          $path = new DOMXPath($doc);
          $path-&gt;registerNamespace('a', 'http://www.w3.org/2005/Atom');
          $path-&gt;registerNamespace('gd', 'http://schemas.google.com/g/2005');
          $nodes = $path-&gt;query('//a:entry');
          $num = $nodes-&gt;length;
  
          $contacts = array();
          for ($x = 0; $x &lt; $num; $x++) {
              $entry = $nodes-&gt;item($x);
              $tnodes = $path-&gt;query('a:title', $entry);
              $nnode = $tnodes-&gt;item(0);
              $name = $nnode-&gt;textContent;
              $enodes = $path-&gt;query('gd:email', $entry);
              $mnode = $enodes-&gt;item(0);
              $email = $mnode-&gt;getAttribute('address');
              // NOTE: Keep in mind that $mnode-&gt;getAttribute('rel') tells you what kind of email it is.
              // NOTE: Also remember that there can be multiple emails per entry!
              if (empty($name)) {
                  $contacts[] = $email;
              } else {
                  $contacts[$name] = $email;
              }
          }        
  
          return $contacts;
      }
  }
  </code></pre>
  
  <p>Line 97 is that <code>$email = $mnode-&gt;getAttribute('address');</code> line near the end.</p>
  
  <p>What can I change here to not get that error anymore and get this working for the Drupal community?  I want to pull in all of a person's contact list, not just 25.  I figure if I sent the number high enough, it'll be close enough to get by.</p>