我在尝试着:
我在(1)中取得了成功,但在(2)中没有取得成功.我收到了来自Google的授权令牌,并将其随后发送给Trends,但Google仍然会返回错误:"您必须登录才能从Google趋势中导出数据":
// http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html
$data = array(
'accountType' => 'GOOGLE',
'Email' => 'my.email@gmail.com',
'Passwd' => 'my.password',
'service' => 'trendspro',
'source' => 'company-application-1.0'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPAUTH, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
preg_match("/Auth=([a-z0-9_\-]+)/i", $response, $matches);
// We now have an authorization-token
$headers = array(
"Authorization: GoogleLogin auth=" . $matches[1],
"GData-Version: 3.0"
);
curl_setopt($ch, CURLOPT_URL, "http://www.google.com/trends/viz?q=MSFT&date=2011-2&geo=all&graph=all_csv&sort=0&sa=N");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, false);
$csv = curl_exec($ch);
curl_close($ch);
// Returns : "You must be signed in to export data from Google Trends"
// Expected: CSV data stream
print_r($csv);
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我发送给Google趋势的auth-token不会被接受或忽略.我不确切知道会发生什么,因为没有给出额外的错误信息.
有谁看到我做错了什么?如果你可以让它工作,意味着谷歌正在返回CSV数据,那么赏金就是你的,我们都有一个圣诞礼物晚期:-)
所以我发现问题与cURL无关.我做的是:
Run Code Online (Sandbox Code Playgroud)SID=DQAAAMUAAADMqt...aYPaYniC_iW LSID=DQAAAMcAAACI5...YDTBDt_xZC9 Auth=DQAAAMgAAABm8...trXgqNv-g0H
Run Code Online (Sandbox Code Playgroud)GData-Version: 3.0 Authorization: GoogleLogin auth=DQAAAMgAAABm8...trXgqNv-g0H
标题:
Run Code Online (Sandbox Code Playgroud)Date: Tue, 27 Dec 2011 00:17:20 GMT Content-Encoding: gzip Content-Disposition: filename=trends.csv Content-Length: 97 X-XSS-Protection: 1; mode=block Server: Google Trends X-Frame-Options: SAMEORIGIN Content-Type: text/csv; charset=UTF-8 Cache-Control: private
数据:
Run Code Online (Sandbox Code Playgroud)You must be signed in to export data from Google Trends
换句话说,我正在http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html上发送Google定义的标题,但没有运气得到正确的回报.关于这个问题,关于这个问题,关于互联网的问题大概就是*没有*.谁知道这里的问题是什么?
检查代码后,问题是 Google Trends 需要SIDkey 而不是Auth. 这是我编写的用于下载 csv 的代码
<?php
header('content-type: text/plain');
// Set account login info
$data['post'] = array(
'accountType' => 'HOSTED_OR_GOOGLE', // indicates a Google account
'Email' => '', // full email address
'Passwd' => '',
'service' => 'trendspro', // Name of the Google service
'source' => 'codecri.me-example-1.0' // Application's name, e.g. companyName-applicationName-versionID
);
$response = xhttp::fetch('https://www.google.com/accounts/ClientLogin', $data);
// Test if unsuccessful
if(!$response['successful']) {
echo 'response: '; print_r($response);
die();
}
// Extract SID
preg_match('/SID=(.+)/', $response['body'], $matches);
$sid = $matches[1];
// Erase POST variables used on the previous xhttp call
$data = array();
// Set the SID in cookies
$data['cookies'] = array(
'SID' => $sid
);
Run Code Online (Sandbox Code Playgroud)
这使用了我的xhttp 类,一个 cURL 包装器。