2010-01-19 27 views
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); 
} 
?> 

結果是找不到頁面。但是,我從firefox調用http://www.google.com/calendar/feeds/default/allcalendars/full,它是返回XML文件。所以,我認爲,我的代碼可能是錯誤的。但我找不到錯誤。 :(使用Google日曆的PHP捲曲

+0

$ _GET [「令牌」] - 它是授權令牌或會話令牌?據我所知,你應該首先請求/帳戶/ AuthSubSessionToken返回一個頭中的會話令牌。然後在所有後續請求中使用該會話令牌。 – Kniganapolke 2010-01-19 09:38:01

+0

嘗試刪除Host標頭,但無論如何它應該被忽略,因爲絕對網址。 – Kniganapolke 2010-01-19 09:58:51

+0

來自https://www.google.com/accounts/AuthSubRequest?scope=http%3A%2F%2Fwww.google.com%2Fcalendar%2Ffeeds%2F&session=1&secure=1&next=http%3A%2F%2Fmydomain.com – saturngod 2010-01-19 14:59:34

回答

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/[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> 

<? 
} 
?> 
+0

我不知道如何使用POST,PUT,DELETE。 :( – saturngod 2010-01-20 03:34:55

0

那是因爲你是通過你個人的端口訪問谷歌日曆。當您訪問特定的URL,谷歌會檢查,如果你已經登錄,如果沒有,它會發送一個404如果你是,它輸出根據您提供的設置日曆。該URL沒有指定它應該從網站拉特定日曆,因爲它是被提取從您的服務器,這將沒有它不能使用存儲在用戶的計算機上的Cookie當我嘗試訪問那個頁面而沒有登錄時,我得到一個401需要授權的錯誤,我敢打賭是PHP正在得到什麼,你只是不知道它。放到您的Google日曆設置中,然後找到嵌入選項來查找一個網址特定於您的帳戶,以便它始終爲您的日曆提取XML Feed。

瞭解更多關於谷歌日曆網址在這裏:http://www.google.com/support/calendar/bin/answer.py?answer=34578

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

+0

我更改地址,並使用我的私人XML日曆地址。我想使用Google Calendar API。我嘗試了http://code.google.com/apis/calendar/data/2.0/developers_guide_protocol.html#Auth – saturngod 2010-01-19 05:22:35

0

我認爲你可以用這條線在頭中重寫URL:

GET /accounts/AuthSubSessionToken HTTP/1.1 

我認爲這樣會將CURL指向http://www.google.com/accounts/AuthSubSessionToken

當您移除它?

+0

同樣的問題。我改變了這個\t「GET http://www.google.com/accounts/AuthSubSessionToken HTTP/1.1」。但錯誤是404。 – saturngod 2010-01-19 05:17:33