2

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

Thanks,

Yosef

Community
  • 1
  • 1
Ben
  • 25,389
  • 34
  • 109
  • 165
  • It's not Zend, but probably this helps: http://stackoverflow.com/questions/6101151/get-google-trends-insights-with-php-login-auth-google – hakre Jun 15 '11 at 11:13
  • i need only zend, this link exist in 'ways' – Ben Jun 15 '11 at 11:49
  • zend has a HTTP client as well. So you could stick to that one. – hakre Jun 15 '11 at 11:59
  • What do you mean by "have to use Zend Framework classes"? Zend IS php and you can extend Zend in any way possible and implement those "other php ways" easily. – Adrian World Jul 03 '11 at 16:06

1 Answers1

1

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%
Fabio
  • 18,856
  • 9
  • 82
  • 114
  • thank you very much - looks great, I going to check how its works – Ben Jul 04 '11 at 13:57
  • How can you add headers to be a browser? – Ben Jul 04 '11 at 14:24
  • `$auth->setHeaders('YourHeader', 'foobar');` see [http client doc](http://framework.zend.com/apidoc/core/_Http_Client.html#Zend_Http_Client::setHeaders()) – Fabio Jul 04 '11 at 14:36