views:

423

answers:

4
<?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. :(

+1  A: 

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&amp;answer=37648

animuson
I change the address and use with my private xml calendar address. I want to use Google Calendar API. I tried from http://code.google.com/apis/calendar/data/2.0/developers_guide_protocol.html#Auth
saturngod
A: 

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?

Cetra
same problem. I change like that "GET http://www.google.com/accounts/AuthSubSessionToken HTTP/1.1" . but error is 404.
saturngod
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=&lt;?= $next ?>&scope=<?= $scope ?>&secure=0&session=1">Click here to authenticate through Google.</a>

<?
}
?>
saturngod
I don't know how to make with POST,PUT,DELETE. :(
saturngod
A: 

I tryed your code but i seem to lack something. the gdata library i dont have it. is it the gdata part of zend or what is it? I proberbly didnt understand zend but it seems like there is now index.php with zend?

Brian