使用 PHP 访问 Google Bookmarks 服务器端 [英] Accessing Google Bookmarks server side with PHP

查看:23
本文介绍了使用 PHP 访问 Google Bookmarks 服务器端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经使用此 PHP 代码访问我的 Google 书签,服务器端:

$curlObj = curl_init();curl_setopt($curlObj, CURLOPT_URL, "https://www.google.com/bookmarks/?output=rss");curl_setopt($curlObj, CURLOPT_USERPWD, "whatever@googlemail.com:mypassword");curl_setopt ($curlObj, CURLOPT_RETURNTRANSFER, 1);$response = curl_exec($curlObj);回声 $response;curl_close($curlObj);

以前,使用上面的代码,我会看到一个 XML 提要.

现在显示302 您的文档已移动.单击此处".该链接将我带到登录页面.

有什么想法吗?

谢谢.

解决方案

此类授权不再有效.您需要通过 https://accounts.google.com/ServiceLogin 进行身份验证,然后获取 https://www.google.com/bookmarks/?output=rss>

示例:

<代码> $value) {$post_string .= $key .'= .urlencode($value) .'&';}$post_string = substr($post_string, 0, -1);curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLoginAuth');curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);$result = curl_exec($ch);$info = curl_getinfo($ch);//echo var_dump($info);if ($info['url']=="https://accounts.google.com/ServiceLoginAuth"){die("登录失败");var_dump($result);} 别的 {curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/bookmarks/?output=rss');curl_setopt($ch, CURLOPT_POST, 0);curl_setopt($ch, CURLOPT_POSTFIELDS, null);$result = curl_exec($ch);var_dump($result);}函数 getFormFields($data){if (preg_match('/(
)/is', $data, $matches)) {$inputs = getInputs($matches[1]);返回 $inputs;} 别的 {die('没有找到登录表单');}}函数 getInputs($form){$inputs = array();$elements = preg_match_all('/(]+>)/is', $form, $matches);如果 ($elements > 0) {for($i = 0; $i < $elements; $i++) {$el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {$name = $name[1];$值 = '';if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) {$value = $value[1];}$inputs[$name] = $value;}}}返回 $inputs;}

I used to access my Google Bookmarks, server side with this PHP code:

$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, "https://www.google.com/bookmarks/?output=rss");
curl_setopt($curlObj, CURLOPT_USERPWD, "whatever@googlemail.com:mypassword");
curl_setopt ($curlObj, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curlObj);
echo $response;
curl_close($curlObj);

Formerly, with the code above, I would have seen an XML feed.

Now it shows "302 Your document has moved. Click Here". The link takes me to a login page.

Any ideas?

Thanks.

解决方案

such authorization is no longer working. you need auth via https://accounts.google.com/ServiceLogin and after get https://www.google.com/bookmarks/?output=rss

example:

<?
$USERNAME = 'aaa';
$PASSWORD = 'bbb';

$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);

curl_setopt($ch, CURLOPT_URL,
  'https://accounts.google.com/ServiceLogin?hl=en&service=bookmarks&continue=http://www.google.com/bookmarks');
$data = curl_exec($ch);

$formFields = getFormFields($data); // my code to get form fields, ask if you want it

$formFields['Email']  = $USERNAME;
$formFields['Passwd'] = $PASSWORD;
unset($formFields['PersistentCookie']);

$post_string = '';
foreach($formFields as $key => $value) {
    $post_string .= $key . '=' . urlencode($value) . '&';
}

$post_string = substr($post_string, 0, -1);

curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLoginAuth');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);

$result = curl_exec($ch);
$info = curl_getinfo($ch);
// echo var_dump($info);
if ($info['url']=="https://accounts.google.com/ServiceLoginAuth")
    {
      die("Login failed");
    var_dump($result);
 } else {
    curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/bookmarks/?output=rss');
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt($ch, CURLOPT_POSTFIELDS, null);

    $result = curl_exec($ch);

    var_dump($result);
}

function getFormFields($data)
{
    if (preg_match('/(<form id=.?gaia_loginform.*?<\/form>)/is', $data, $matches)) {
        $inputs = getInputs($matches[1]);

        return $inputs;
    } else {
        die('didnt find login form');
    }
}

function getInputs($form)
{
    $inputs = array();

    $elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);

    if ($elements > 0) {
        for($i = 0; $i < $elements; $i++) {
            $el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);

            if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {
                $name  = $name[1];
                $value = '';

                if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) {
                    $value = $value[1];
                }

                $inputs[$name] = $value;
            }
        }
    }

    return $inputs;
}

这篇关于使用 PHP 访问 Google Bookmarks 服务器端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆