本文介绍了PHP Curl与Google日历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

结果找不到页面。但是,我调用 from firefox,它返回XML文件。所以,我认为,我的代码可能错了。但我找不到错误。 :(

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

推荐答案

我得到了....我这样改变了

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>

<?
}
?>

这篇关于PHP Curl与Google日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 00:57