PHP Curl 与 Google 日历

发布于 2024-08-18 11:49:05 字数 1129 浏览 3 评论 0原文

<?php


if(isset($_GET['token']))
{


    $url="http://www.google.com/calendar/feeds/default/allcalendars/full";
    $useragent="PHP 5.2";
    $header=array(  "GET /accounts/AuthSubSessionToken HTTP/1.1",
                    "Content-Type: application/x-www-form-urlencoded",
                    "Authorization: AuthSub token=".$_GET['token'],
                    "User-Agent: PHP/5.2",
                    "Host: https://www.google.com",
                    "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2",
                    "Connection: keep-alive"
                );

    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_URL, $url); 

    $data = curl_exec($ch); 

    print_r($data);
}
?>

结果是找不到页面。不过,我调用 http://www.google.com/calendar/feeds/来自 firefox 的 default/allcalendars/full ,它返回 XML 文件。所以,我认为,我的代码可能是错误的。但我找不到错误。 :(

<?php


if(isset($_GET['token']))
{


    $url="http://www.google.com/calendar/feeds/default/allcalendars/full";
    $useragent="PHP 5.2";
    $header=array(  "GET /accounts/AuthSubSessionToken HTTP/1.1",
                    "Content-Type: application/x-www-form-urlencoded",
                    "Authorization: AuthSub token=".$_GET['token'],
                    "User-Agent: PHP/5.2",
                    "Host: https://www.google.com",
                    "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2",
                    "Connection: keep-alive"
                );

    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_URL, $url); 

    $data = curl_exec($ch); 

    print_r($data);
}
?>

The result is page not found. However, I call http://www.google.com/calendar/feeds/default/allcalendars/full from firefox , it's return XML file. So, I think, my code may wrong. But I can't find the error. :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

じее 2024-08-25 11:49:05

这是因为您正在通过个人端口访问 Google 日历。每当您访问该特定 URL 时,Google 都会检查您是否已登录。如果没有,它会发送 404。如果您已登录,它会根据您提供的设置输出日历。该 URL 没有指定应从站点提取的特定日历,并且它无法使用存储在用户计算机上的 cookie,因为它是从您的服务器获取的,而服务器不会有任何日历 cookie。当我尝试在不登录的情况下访问该页面时,我收到 401 Authorization Needed 错误,我敢打赌这就是 PHP 所得到的,但你只是没有意识到。

您需要进入 Google 日历设置并找到嵌入选项来查找特定于您的帐户的 URL,以便它始终为您的日历获取 XML 提要。

请在此处详细了解 Google“日历地址”:http:// /www.google.com/support/calendar/bin/answer.py?answer=34578

从其他应用程序查看:http://www.google.com/support/calendar/bin/answer.py?hl=zh-CN&answer=37648

That is because you are accessing Google Calendar via your personal port. Whenever you access that specific URL, Google checks to see if you are logged in. If not, it sends a 404. If you are, it outputs the calendar based on the settings you provided. That URL does not specify a specific calendar that it's supposed to pull from the site, and it cannot use the cookies stored on the user's computer because it is being fetched from your server, which will not have any cookies for a calendar. When I try to access that page without logging on, I get a 401 Authorization Required error, which I bet is what PHP is getting and you just don't realize it.

You need to go into your Google Calendar settings and find the embedding options to find a URL that is specific to your account so that it will always fetch an XML feed for your calendar.

Read more about the Google 'Calendar Address' here: http://www.google.com/support/calendar/bin/answer.py?answer=34578

View from other applications: http://www.google.com/support/calendar/bin/answer.py?hl=en&answer=37648

如歌彻婉言 2024-08-25 11:49:05

我认为您可能会在标头中使用此行覆盖 URL:

GET /accounts/AuthSubSessionToken HTTP/1.1

我认为这会将 CURL 指向 http ://www.google.com/accounts/AuthSubSessionToken

删除它后会发生什么?

I think that you may be overriding the URL with this line in the header:

GET /accounts/AuthSubSessionToken HTTP/1.1

I think that will point CURL to http://www.google.com/accounts/AuthSubSessionToken

What happens when you remove it?

别把无礼当个性 2024-08-25 11:49:05

我明白了....我就这样变了

<?php

function make_api_call($url, $token)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $curlheader[0] = sprintf("Authorization: AuthSub token=\"%s\"/n", $token);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $curlheader);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

function get_session_token($onetimetoken) {
    $output = make_api_call("https://www.google.com/accounts/AuthSubSessionToken", $onetimetoken);

    if (preg_match("/Token=(.*)/", $output, $matches))
    {
        $sessiontoken = $matches[1];
    } else {
        echo "Error authenticating with Google.";
        exit;
    }
    return $sessiontoken;
}



if(isset($_GET['token']))
{
$sessiontoken=get_session_token($_GET['token']);
$accountxml = make_api_call("http://www.google.com/m8/feeds/contacts/[email protected]/full", $sessiontoken);
print_r($accountxml);

}
else
{
$next=urlencode("http://www.mysteryzillion.org/gdata/index.php");
$scope=urlencode("http://www.google.com/m8/feeds/contacts/[email protected]/full");
?>
<a href="https://www.google.com/accounts/AuthSubRequest?next=<?= $next ?>&scope=<?= $scope ?>&secure=0&session=1">Click here to authenticate through Google.</a>

<?
}
?>

I got it.... I changed like this

<?php

function make_api_call($url, $token)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $curlheader[0] = sprintf("Authorization: AuthSub token=\"%s\"/n", $token);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $curlheader);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

function get_session_token($onetimetoken) {
    $output = make_api_call("https://www.google.com/accounts/AuthSubSessionToken", $onetimetoken);

    if (preg_match("/Token=(.*)/", $output, $matches))
    {
        $sessiontoken = $matches[1];
    } else {
        echo "Error authenticating with Google.";
        exit;
    }
    return $sessiontoken;
}



if(isset($_GET['token']))
{
$sessiontoken=get_session_token($_GET['token']);
$accountxml = make_api_call("http://www.google.com/m8/feeds/contacts/[email protected]/full", $sessiontoken);
print_r($accountxml);

}
else
{
$next=urlencode("http://www.mysteryzillion.org/gdata/index.php");
$scope=urlencode("http://www.google.com/m8/feeds/contacts/[email protected]/full");
?>
<a href="https://www.google.com/accounts/AuthSubRequest?next=<?= $next ?>&scope=<?= $scope ?>&secure=0&session=1">Click here to authenticate through Google.</a>

<?
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文