phpzend-frameworkzend-gdata

How download cvs file with google authorization in google insights - have to use zend classes and not other php ways (zend_gbase etc.. )


How download cvs file with google authorization in google trends service - have to use zend framework classes and not other php ways .

Thanks,

Yosef


Solution

  • This could be what you need. You need to enter your Google username and password in the $email and $passwd variables and to setup the autoloader or require ZF classes.

    References: Zend_Gdata_ClientLogin and ClientLogin

    // setup the autoloader first or require used classes
    // using the example code in the example to get auth tokens
    try {
        $client = Zend_Gdata_ClientLogin::getHttpClient($email, $passwd, 'xapi', $httpClient);
    } 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";
    }
    
    // extract authentication tokens from response body
    $ck_count = preg_match_all('/^(.+)=(.+)$/m', $client->getLastResponse()->getBody(), $cookies);
    
    // build a new http client
    $auth = new Zend_Http_Client('http://www.google.com/trends/viz?q=facebook&graph=all_csv&sa=N');
    
    for ($i = 0; $i < $ck_count; $i++) {
        // authenticate it using the provided tokens as cookies
        $auth->setCookie($cookies[1][$i], $cookies[2][$i]);
    }
    // make the request and dump cvs data
    Zend_Debug::dump($auth->request()->getBody());
    

    Output given (truncated for brevity):

    facebook, facebook (std error)
    1.00, 0%
    
    
    Week, facebook, facebook (std error)
    Jan 4 2004, 0, >10%
    Jan 11 2004, 0, >10%
    Jan 18 2004, 0, >10%
    Jan 25 2004, 0, >10%
    Feb 1 2004, 0, >10%
    Feb 8 2004, 0, >10%
    Feb 15 2004, 0, >10%
    Feb 22 2004, 0, >10%
    Feb 29 2004, 0, >10%