用curl登录并获取会话

时间:2011-05-23 18:12:54

标签: php api curl

你好我需要以某种方式从

获得最高的区域兴趣和兴趣

http://www.google.com/trends?q=lingerie+&ctab=0&geo=id&date=all&sort=0

或更好

http://www.google.com/insights/search/#q=lingerie&geo=ID&cmpt=q

所以我发现我们必须登录导出数据有人能用我们的谷歌用户名和密码给我一个例子吗?也许使用curl导出数据?或其他什么

感谢您查看

Adam Ramadhan

3 个答案:

答案 0 :(得分:5)

为了快速了解它,我使用了xhttp class这是一个卷曲包装器,我认为代码很容易遵循。

<?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
);

$response = xhttp::fetch('http://www.google.com/insights/search/overviewReport?q=lingerie&geo=ID&cmpt=q&content=1&export=1', $data);

// CSV data in the response body
echo $response['body'];

?>

结果位于:http://codecri.me/apps/test/test.google.trends.php 但我不确定结果是否符合您的要求。

答案 1 :(得分:1)

自2012年4月起,Google更改了其身份验证政策,编辑Arvin的代码如下:

// Extract Auth
preg_match('/Auth=(.+)/', $response['body'], $matches);
$auth = $matches[1];

// Erase POST variables used on the previous xhttp call
$data = array();

// Set the Authorization header
$data['headers'] = array(
    'Authorization' => 'GoogleLogin auth='.$auth
);

答案 2 :(得分:0)

curl手册页介绍了如何提供用户名和密码。你试过-u标志吗?

-u / - 用户        指定用于服务器身份验证的用户名和密码。覆盖-n / - netrc和--netrc-optional。

   If you just give the user name (without entering a colon) curl will prompt for a password.

   If you use an SSPI-enabled curl binary and do NTLM authentication, you can force curl to pick up the user name  and  password from your environment by simply specifying a single colon with this option: "-u :".

   If this option is used several times, the last one will be used.