问题描述
如何在Google趋势服务中通过Google授权下载cvs文件-必须使用zend框架类而不是其他PHP方式。
谢谢,
约瑟夫
解决方法
这可能就是您所需要的。您需要在
$email
和$passwd
变量中输入Google用户名和密码,并设置自动装带器或需要ZF类。
参考:Zend_Gdata_ClientLogin和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());
给定的输出(为简洁起见,将其截断):
facebook,facebook (std error)
1.00,0%
Week,facebook,facebook (std error)
Jan 4 2004,>10%
Jan 11 2004,>10%
Jan 18 2004,>10%
Jan 25 2004,>10%
Feb 1 2004,>10%
Feb 8 2004,>10%
Feb 15 2004,>10%
Feb 22 2004,>10%
Feb 29 2004,>10%