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);
}
?>
结果是找不到页面。不过,我调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为您正在通过个人端口访问 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
我认为您可能会在标头中使用此行覆盖 URL:
我认为这会将 CURL 指向 http ://www.google.com/accounts/AuthSubSessionToken
删除它后会发生什么?
I think that you may be overriding the URL with this line in the header:
I think that will point CURL to http://www.google.com/accounts/AuthSubSessionToken
What happens when you remove it?
我明白了....我就这样变了
I got it.... I changed like this