title: PHP GMAIL Contacts XML Parsing with DOMDocument and cURL
body: <p>What I am trying to get currently is just the attribute of gd:email that is the "address=" only nothing else as of the moment. Which I can get to the xml portion, heck I can even get any given think per say as long as its within tags like   but to get the attribute of any given one like in my case..  I am completely confused on. I used to know how to do it but its been so long since I did anything that wasn't simple for XML useage. so I done messed my own self up..</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;
      &lt;feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gContact='http://schemas.google.com/contact/2008' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gd='http://schemas.google.com/g/2005'&gt;
          &lt;id&gt;xxxxxxxxxxxxxx@gmail.com&lt;/id&gt;
          &lt;updated&gt;2011-06-30T00:07:48.706Z&lt;/updated&gt;
          &lt;category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/&gt;
          &lt;title type='text'&gt;Taco Bells's Contacts&lt;/title&gt;
          &lt;link rel='alternate' type='text/html' href='http://www.google.com/'/&gt;
          &lt;link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full'/&gt;
          &lt;link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full'/&gt;
          &lt;link rel='http://schemas.google.com/g/2005#batch' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/batch'/&gt;
          &lt;link rel='self' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full?max-results=5'/&gt;
          &lt;link rel='next' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full?start-index=6&amp;max-results=5'/&gt;
          &lt;author&gt;
              &lt;name&gt;Taco Bell&lt;/name&gt;
              &lt;email&gt;xxxxxxxxxxxxxx@gmail.com&lt;/email&gt;
          &lt;/author&gt;
          &lt;generator version='1.0' uri='http://www.google.com/mate/feeds'&gt;Contacts&lt;/generator&gt;
          &lt;openSearch:totalResults&gt;90&lt;/openSearch:totalResults&gt;
          &lt;openSearch:startIndex&gt;1&lt;/openSearch:startIndex&gt;
          &lt;openSearch:itemsPerPage&gt;5&lt;/openSearch:itemsPerPage&gt;
          &lt;entry&gt;
              &lt;id&gt;http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/base/0&lt;/id&gt;
              &lt;updated&gt;2010-01-27T00:11:57.430Z&lt;/updated&gt;
              &lt;category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/&gt;
              &lt;title type='text'&gt;Taco B&lt;/title&gt;
              &lt;link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='http://www.google.com/mate/feeds/photos/media/xxxxxxxxxxxxxx%40gmail.com/0/8_XL_JoAPon1k7SNixI2iA'/&gt;
              &lt;link rel='http://schemas.google.com/contacts/2008/rel#photo' type='image/*' href='http://www.google.com/mate/feeds/photos/media/xxxxxxxxxxxxxx%40gmail.com/0'/&gt;
              &lt;link rel='self' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/0'/&gt;
              &lt;link rel='edit' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/0/1264551117430000'/&gt;
              &lt;gd:email rel='http://schemas.google.com/g/2005#other' address='xxxxxxxxxxxxxx@gmail.com' primary='true'/&gt;
          &lt;/entry&gt;
          &lt;entry&gt;
              &lt;id&gt;http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/base/1&lt;/id&gt;
              &lt;updated&gt;2007-08-01T18:02:04.410Z&lt;/updated&gt;
              &lt;category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/&gt;
              &lt;title type='text'&gt;&lt;/title&gt;
              &lt;link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='http://www.google.com/mate/feeds/photos/media/xxxxxxxxxxxxxx%40gmail.com/1/1B2M2Y8AsgTpgAmY7PhCfg'/&gt;
              &lt;link rel='self' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/1'/&gt;
              &lt;link rel='edit' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/1/1185991324410001'/&gt;
              &lt;gd:email rel='http://schemas.google.com/g/2005#other' address='xxxxxxxxxxxxxx@sbcglobal.net' primary='true'/&gt;
          &lt;/entry&gt;
          &lt;entry&gt;
              &lt;id&gt;http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/base/2&lt;/id&gt;
              &lt;updated&gt;2010-01-27T00:11:57.430Z&lt;/updated&gt;
              &lt;category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/&gt;
              &lt;title type='text'&gt;Steve Sattler&lt;/title&gt;
              &lt;link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='http://www.google.com/mate/feeds/photos/media/xxxxxxxxxxxxxx%40gmail.com/2/1B2M2Y8AsgTpgAmY7PhCfg'/&gt;
              &lt;link rel='self' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/2'/&gt;
              &lt;link rel='edit' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/2/1264551117430000'/&gt;
              &lt;gd:email rel='http://schemas.google.com/g/2005#other' address='xxxxxxxxxxxxxx@yahoo.com' primary='true'/&gt;
          &lt;/entry&gt;
          &lt;entry&gt;
              &lt;id&gt;http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/base/3&lt;/id&gt;
              &lt;updated&gt;2010-01-27T00:11:57.430Z&lt;/updated&gt;
              &lt;category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/&gt;
              &lt;title type='text'&gt;Michael Montana&lt;/title&gt;
              &lt;link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='http://www.google.com/mate/feeds/photos/media/xxxxxxxxxxxxxx%40gmail.com/3/1B2M2Y8AsgTpgAmY7PhCfg'/&gt;
              &lt;link rel='self' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/3'/&gt;
              &lt;link rel='edit' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/3/1264551117430000'/&gt;
              &lt;gd:email rel='http://schemas.google.com/g/2005#other' address='xxxxxxxxxxxxxx@comcast.net' primary='true'/&gt;
          &lt;/entry&gt;
          &lt;entry&gt;
              &lt;id&gt;http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/base/4&lt;/id&gt;
              &lt;updated&gt;2007-08-01T18:02:04.410Z&lt;/updated&gt;
              &lt;category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/&gt;
              &lt;title type='text'&gt;&lt;/title&gt;
              &lt;link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='http://www.google.com/mate/feeds/photos/media/xxxxxxxxxxxxxx%40gmail.com/4/1B2M2Y8AsgTpgAmY7PhCfg'/&gt;
              &lt;link rel='self' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/4'/&gt;
              &lt;link rel='edit' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/4/1185991324410001'/&gt;
              &lt;gd:email rel='http://schemas.google.com/g/2005#other' address='xxxxxxxxxxxxxx@courant.com' primary='true'/&gt;
          &lt;/entry&gt;
      &lt;/feed&gt;
  </code></pre>
  
  <p>with code that looks like (this is the whole script (currently))</p>
  
  <pre><code>$user = "xxxxxxxxxxxxxxxxxxx@gmail.com";
  $password = "xxxxxxxxxxxx";
  
  // ref: http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html
  
  // step 1: login
  $login_url = "https://www.google.com/accounts/ClientLogin";
  $fields = array(
      'Email' =&gt; $user,
      'Passwd' =&gt; $password,
      'service' =&gt; 'cp', // &lt;== contact list service code
      'source' =&gt; 'test-google-contact-grabber',
      'accountType' =&gt; 'GOOGLE',
  );
  
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL,$login_url);
  curl_setopt($curl, CURLOPT_POST, 1);
  curl_setopt($curl, CURLOPT_POSTFIELDS,$fields);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
  $result = curl_exec($curl);
  
  $returns = array();
  
  foreach (explode("\n",$result) as $line)
  {
      $line = trim($line);
      if (!$line) continue;
      list($k,$v) = explode("=",$line,2);
  
      $returns[$k] = $v;
  }
  
  curl_close($curl);
  
  // step 2: grab the contact list
  $feed_url = "http://www.google.com/m8/feeds/contacts/$user/full?&amp;max-results=5";
  
  $header = array(
      'Authorization: GoogleLogin auth=' . $returns['Auth'],
  );
  
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $feed_url);
  curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
  
  $result = curl_exec($curl);
  curl_close($curl);
  
  $doc = new DOMDocument();
  $doc-&gt;load($result);
  $arrFeeds = array();
  foreach ($doc-&gt;getElementsByTagName('entry') as $node) {
      $monkey = $node-&gt;getAttribute('{gd:email}');
      $itemRSS = array($monkey-&gt;nodeValue);
  array_push($arrFeeds, $itemRSS);
  }
  array_unique($arrFeeds);
  </code></pre>