PHP卷曲与谷歌日历

时间:2010-01-19 03:48:53

标签: php curl calendar

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

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

3 个答案:

答案 0 :(得分:0)

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

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

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

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

答案 1 :(得分:0)

我认为您可能会在标题中覆盖此行的URL:

GET /accounts/AuthSubSessionToken HTTP/1.1

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

删除后会发生什么?

答案 2 :(得分:0)

我明白了......我改变了这个

<?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/yourmail@gmail.com/full", $sessiontoken);
print_r($accountxml);

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

<?
}
?>