如何在谷歌洞察中使用谷歌授权下载cvs文件 - 必须使用zend类而不是其他php方式(zend_gbase等...)

时间:2011-06-15 09:18:51

标签: php zend-framework zend-gdata

如何在谷歌趋势服务中使用谷歌授权下载cvs文件 - 必须使用zend框架类而不是其他php ways

谢谢,

约瑟夫

1 个答案:

答案 0 :(得分:1)

这可能就是你所需要的。您需要在$email$passwd变量中输入您的Google用户名和密码,然后设置自动加载器或需要ZF类。

参考文献:Zend_Gdata_ClientLoginClientLogin

// 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());

给出的输出(为简洁而截断):

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%