Get Your Google Accounts data in XML - Contacts, Reader etc

Discussion in 'PHP' started by anjanesh, Jul 4, 2007.

  1. #1
    Hi

    You can get a lot of your google service info in XML format once you're logged in. Its kinda like an API but some of it like Reader API is not disclosed openly.

    Contact list sorted by affinity though. Wanted to share this as its been searched around a lot plus you get other data too like reader subscription-list etc.

    
    <?php
    // Retrieving many of google's data is easy once logged in since they provide a lot of their data in XML.
    set_time_limit(100);
    error_reporting(E_ALL);
    
    $c = getGmailContacts("username@gmail.com", "password", $Err);
    
    if (!$c === FALSE)
     echo $c;
    else
     print_r($Err);
    
    function getGmailContacts($Username, $Password, &$Err)
     {
            $ch = curl_init();
    
            curl_setopt ($ch, CURLOPT_POST, TRUE);
            curl_setopt ($ch, CURLOPT_HEADER, FALSE);
            curl_setopt ($ch, CURLOPT_USERAGENT, "ASO");
            curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt ($ch, CURLOPT_REFERER, "");
            curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, TRUE);
            curl_setopt ($ch, CURLOPT_MAXREDIRS, 5);
            curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
            curl_setopt ($ch, CURLOPT_FAILONERROR, 1);
            curl_setopt ($ch, CURLOPT_COOKIESESSION, TRUE); # PHP5 only
            curl_setopt ($ch, CURLOPT_COOKIEFILE, "cookie.txt");
            curl_setopt ($ch, CURLOPT_COOKIEJAR,  "cookie.txt");
            curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
    
            curl_setopt ($ch, CURLOPT_URL, "https://www.google.com/accounts/LoginAuth?continue=http%3A%2F%2Fwww.google.com%2F&hl=en");
    
            $Data = "";
            $Data .= "continue=".urlencode("http://www.google.com/")."&";
            $Data .= "nui=13&";
            $Data .= "Email=".urlencode($Username)."&";
            $Data .= "Passwd=".urlencode($Password)."&";
            // $Data .= "PersistentCookie=Yes&";
            $Data .= "rmShown=1&";
            $Data .= "signIn=Sign+in&";
    
            curl_setopt ($ch, CURLOPT_POSTFIELDS, $Data);
    
            $contents = curl_exec($ch);
            $ErrNo = curl_errno($ch);
    
            if ($ErrNo != 0)
             {
                    $Err[] = "cURL Error @ Login :: Error No: ".curl_errno($ch).' : '.curl_error($ch);
                    return FALSE;
             }
    
            if ($contents == NULL)
             {
                    $Err[] = "contents == NULL @ Login";
                    return FALSE;
             }
    
            curl_setopt ($ch, CURLOPT_POST, FALSE);
            $url = "http://www.google.com/reader/api/0/subscription/list";
            $url = "http://www.google.com/notebook/contacts";
            curl_setopt ($ch, CURLOPT_URL, $url);
    
            $contents = curl_exec($ch);
            $ErrNo = curl_errno($ch);
    
            if ($ErrNo != 0)
             {
                    $Err[] = "cURL Error @ Contacts :: Error No: ".curl_errno($ch).' : '.curl_error($ch);
                    return FALSE;
             }
    
            if ($contents == NULL)
             {
                    $Err[] = "contents == NULL @ Contacts";
                    return FALSE;
             }
    
            return $contents;
     }
    ?>
    
    PHP:
    This doesnt seem to work with Google Apps.
     
    anjanesh, Jul 4, 2007 IP