iPhone:如何让 safari 识别 vcard?

发布于 2024-08-14 16:41:44 字数 498 浏览 2 评论 0原文

我正在尝试创建一个二维码,以便 iPhone 用户可以导入我的地址簿信息。我的做法是:

  • 将 VCF (vcard) 文件放在我的 Web 服务器上,
  • 创建包含此 URL 的 QR 图像。

http://markharrison.net/mh-vcf-small.png

这适用于我的桌面浏览器(它使用地址簿应用程序打开电子名片)。

在 iPhone 上,QR 阅读器成功告诉 safari 访问 vcard,但 safari 抱怨它不知道如何处理 vcard。我已确认 Content-Type: text/x-vcard 正在发送。

所以,我的问题是:

  • 如何让 Safari 识别我的 vcard?
  • Safari 是否可以识别其他卡格式?

I'm trying to create a QR code so that iPhone users can import my address book information. I'm doing this by:

  • putting a VCF (vcard) file on my web server
  • creating a QR image that contains this URL.


http://markharrison.net/mh-vcf-small.png

This is working on my desktop browser (it opens the vcard with the address book app).

On the iPhone, the QR reader successfully tells safari to access the vcard, but then safari complains it does not know how to handle the vcard. I've confirmed that Content-Type: text/x-vcard is being sent.

So, my questions:

  • How do I get Safari to recognize my vcard?
  • Is there another card format that safari recognizes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情绪操控生活 2024-08-21 16:41:44

[更新 - 2013 年 9 月 - iOS7 现在支持直接下载 VCARD 文件并导入到本机联系人应用程序中]

function isiOS7($user_agent=NULL) {
    if(!isset($user_agent)) {
        $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
    }
    return (strpos($user_agent, 'OS 7') !== FALSE);
}

# Output file contents - simple version
if(!isIphone() || isiOS7()) {
    # Send correct headers      
    header("Content-type: text/x-vcard; charset=utf-8"); 
                // Alternatively: application/octet-stream
                // Depending on the desired browser behaviour
                // Be sure to test thoroughly cross-browser

    header("Content-Disposition: attachment; filename=\"iphonecontact.vcf\";");
    # Output file contents 
    echo file_get_contents("iphonecontact.vcf");
    exit();
}

以下是通过 Mobile Safari 将联系人从网页下载到 iPhone 的替代方法的完整说明。基本思想是将所需的联系信息作为文件附加到由 mobile safari 处理的日历事件中。联系人文件本身使用简单的 PHP 脚本在日历文件中动态进行 base64 编码。所以请继续阅读...

VCALENDAR 文件中嵌入 VCARD 的基本布局

只是想要源代码吗?在此处下载 [iphone-contact-download-demo],以获得完整运行的 HTML5 Web 应用程序,您可以自由复制和复制或访问 http://iphone.mobicontact.info 查看工作演示。该演示使用 HTML5 缓存清单,将内容下载到您的 iPhone 或支持 HTML5 的浏览器,以便在离线时使用。如果您想了解更多信息,请谷歌搜索有关“离线网络应用程序”的更多详细信息。

VCALENDAR 中嵌入 VCARD for iPhone 下载
您可能已经了解到,无法使用 Mobile Safari 从网页将联系人文件(VCARD 格式数据作为 .vcf 文件)直接下载到您的 iPhone。浏览器只是无法将 .vcf 扩展名和 mime 类型 (text/x-vcard) 识别为它应该处理的内容。顺便说一句,Android 和大多数其他移动设备应该能够足够轻松地处理 VCARD 文件 - 标准本身就像山一样古老!

您可能还读过,可以通过请求用户的电子邮件地址,然后通过电子邮件将联系文件发送给他们,或者创建指向 Google 地图条目的链接并从中提取联系信息(Google 地图条目需要数周时间才能获得)来实现可行的目标。在英国)。

虽然这些都是可行的解决方案,但它们并不是我所说的用户友好型解决方案,我尝试让我的客户接受其中任何一个以在移动网络应用程序上下载联系人信息,但没有成功。

现在,我还应该提到,您可以下载一些添加对 VCARD (.vcf) 文件支持的第三方应用程序 - 例如 QRAFTER 和 VCARD GETTER,它们均来自 Kerem Erkan 出色的 QR 阅读器及其关于该主题和 iPad 解决方案的博客,和 HIPSCAN vcard 导入器。但在我看来,假设你的读者安装了这些应用程序就太过分了,所以我寻找电子邮件、谷歌地图和第三方应用程序的替代解决方案。

在我继续之前,有许多链接更详细地描述了该问题:

堆栈溢出有几个关于该主题的线程
强制下载 vCard (thesheep.co.uk)
EMAIL 解决方案和相关博客可从 Code Train 此处下载 vCard 选项
这里还有一个使用谷歌地图的可能解决方案。 Dataplex 的这篇文章也涵盖了这一点。
Jonas Schmid 谈论如何正确提供文件类型。
MacRumours 线程

然后我开始思考,从 IOS5 开始,iPhone 确实支持从网页下载 vcalendar 文件。 VCALENDAR 文件通常具有 .ics 扩展名,并由 mobile safari 处理,并打开一个窗口,可以在其中打开文件并将其保存到日历中。我觉得令人难以置信的是,Apple 和 Mobile Safari 支持 VCAL 文件,但不支持 VCARD 文件,但事实就是如此。那么如果我可以将 VCARD 文件附加到 VCALENDAR 文件呢?

第一个障碍是在日历事件上获取附件...

最初,我尝试在 Windows 中向 Outlook 日历约会添加附件,但从网页链接时可能无法正确下载,并且肯定没有产生我想要的结果。所以,我决定尝试使用 Apple 软件,毕竟我们正在尝试下载到 iPhone 上。使用 Apple 的默认日历应用程序“iCal”会带来一个基本问题 - 您无法向事件/日历约会添加附件!因此,我在谷歌上搜索了“在 OSX 中向日历事件添加附件”或类似内容,并找到了这篇优秀的文章,它为我指明了正确的方向。

在 OSX 中向日历约会添加附件。

几分钟后,安装了 BusyCal 的副本后,我就能够创建一个事件并向其附加 VCARD 文件(之前从我的联系人中保存/导出)在 OSX 中)。此步骤仅需要了解将附件添加到日历项目时创建的文件的格式 - 您不需要安装 BusyCal 来实现下面描述的解决方案,但我将其包含在内以供参考,以便您可以了解 VCARD 是如何实现的嵌入在 VCALENDAR/VEVENT (.ics) 文件中。

我使用的步骤是在运行 OSX Lion 的 Apple iMac 上进行的:

从联系人/地址簿导出联系人以创建 VCARD 文件 (.vcf) - 您可以使用文本编辑器编辑此文件以删除所有额外的内容,例如UID 和 PROD-ID(如果您愿意)。
开始:VCARD
版本:3.0
N:联系人;iPhone;;;
FN:iPhone 联系方式
EMAIL;type=INTERNET;type=WORK;type=pref:[电子邮件受保护]
电话;类型=手机;类型=语音;类型=首选项:012-345-6789
结束:VCARD

创建一个新日历 - 随意称呼它,我在“在我的 Mac 上”区域中使用了“vcal”,这样当您导出此日历以生成 .ics 文件时,您得到的只是带有附加卡片的单个事件而不是使用现有日历时可能发生的所有事件。
创建一个新事件 - 随意命名 - 并指定任意时间和日期。
将 (1) 中的 VCARD 文件附加到此新事件 - 请参阅屏幕截图。iMac 上的 BusyCal 允许附加 VCALENDAR 约会
将事件保存到日历中。
从 BusyCal 的主菜单中,将日历导出到本地磁盘上的 .ics 文件 - 在此处下载 zip 文件 --> Apple 日历活动并附有联系人文件。
现在,您可以使用您最喜欢的文本编辑器来检查 Apple 如何在日历事件中存储附件,结果是使用:ATTACH;VALUE=BINARY;ENCODING=BASE64;FMTTYPE=text/directory;
X-APPLE-FILENAME=iPhone 联系人.vcf:
QkVHSU46VkNBUkQNClZFUlNJT046M...等...[base64 编码的 VCARD]
因此,我随后删除了所有不需要的额外内容(此处反复试验),直到获得 iPhone 仍能将其识别为带有附件的有效日历事件的绝对最小值。这样做的原因是为了使在最终网页上创建日历事件的 PHP 文件尽可能简单 - 这是我将其简化为的 vcal-minimal。

因此,正如我所说,上述所有内容都不是实现联系人下载解决方案所必需的 - 我只是想向您展示我如何了解 Apple 如何使用 X-APPLE-FILENAME 联系线附加文件。当然,还有他们如何使用 Base64 编码对 VCARD 数据进行编码。现在我们已经拥有了即时创建 VEVENT 并将 VCARD 附加到其上所需的所有信息,这些信息可以通过 Mobile Safari 直接下载到 iPhone。

1) 上传您想要下载的联系人文件 (.vcf) - 您将需要此文件,以便您可以将其直接下载到非 iPhone 或针对 iPhone 对其进行 Base64 编码。

2) 创建一个指向 PHP 文件的链接,该文件将动态生成日历事件,例如:
也许可以在 HTML5 移动应用程序中下载 Contact iPhone,如下所示:
简单链接到 PHP 文件以下载 VCALENDAR 中的联系人

3) 使用此处的代码创建或上传 vcal.php iphonecontact -源代码。此 PHP 文件为要下载的日历文件应用正确的标头/内容类型,然后您可以选择直接获取日历文件内容“iphonecontact.ics”(如“vcal-from-file.php”中所做的那样)或生成动态日历如“vcal.php”所示。后者是我的首选方法,因为您会得到一个很好的带时间戳的日历事件,显示下载的时间和日期。

4)就是这样!一切就绪 - 现在在您的 iPhone 上浏览网页并单击链接来执行“vcal.php”。您的浏览器现在应该显示 iphonecontact.ics 文件并要求您在手机日历应用程序中打开它
从网页下载的日历约会

5)选择“打开方式...”,您将看到日历约会并所附的联系文件。
带有附加联系人文件的日历事件

6) 请注意我如何将日历事件的标题设置为有用的内容,以告诉用户什么处理嵌入的联系人文件(您可以在 vcal.php 中看到为事件设置 SUMMARY 字段的行)。现在单击附加的联系文件...
打开的联系人文件

7) 然后“创建新联系人”,您就快到了...
联系人已保存!!!

保存联系人并在苹果让您跳过的圈子里低声咒骂!

现在,根据我在 HTML5 Web 应用程序上使用此技术的经验,我想在此提及几点:

使用缓存清单 - 我在提供日历文件 (iphonecontact.ics) 时遇到了一些意外的行为/问题,如果它已被缓存 - 我只是无法让它工作,所以我将其从清单中排除,这意味着它总是被下载 - 源代码包含在这个 zip 文件 iphonecontact-source-code 中。

当然,您可以进行一些用户代理嗅探,以在“vcal.php”PHP 文件中检测是否将 VCALENDAR 文件提供给 iPhone,并将 VCARD 文件本身提供给所有其他浏览器。这是一个足够简单的检查,并且通过谷歌搜索进行了详细记录,因此我将把它留给您来解决,如果您需要的话,并且如果您需要的话,请随意在此处发布代码。

我认为就是这样 - 总结一下:

Mobile safari 不直接支持 VCARD (.vcf) 文件,但支持 VCALENDAR (.ics) 文件。

  • 目前最好的解决方案是通过请求用户的电子邮件地址来向联系人发送电子邮件,或者将联系人嵌入到谷歌地图链接中,或者下载处理 VCARDS 的应用程序。
  • Apple 确实支持日历文件附件,但并不容易,因此一旦我们知道如何完成此操作,我们就可以在 PHP 中实现。
  • 将 VCARD 嵌入到 VCALENDAR 文件中,用户只需额外单击一两次即可将联系人保存到其地址簿中。

我希望您喜欢这个解决方案 - 在 Apple 做出让步并允许 Mobile Safari 接受 VCARD 文件之前,它已经是我认为最好的解决方案了。

下次见...

[UPDATED - Sep 2013 - iOS7 now supports direct download of VCARD files and import into the native Contacts application]

function isiOS7($user_agent=NULL) {
    if(!isset($user_agent)) {
        $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
    }
    return (strpos($user_agent, 'OS 7') !== FALSE);
}

# Output file contents - simple version
if(!isIphone() || isiOS7()) {
    # Send correct headers      
    header("Content-type: text/x-vcard; charset=utf-8"); 
                // Alternatively: application/octet-stream
                // Depending on the desired browser behaviour
                // Be sure to test thoroughly cross-browser

    header("Content-Disposition: attachment; filename=\"iphonecontact.vcf\";");
    # Output file contents 
    echo file_get_contents("iphonecontact.vcf");
    exit();
}

What follows is a full description of an alternative method for downloading contacts from a web page to an iPhone via Mobile Safari. The basic idea is to attach the required contact information as a file in a calendar event which IS handled by mobile safari. The contact file itself is base64 encoded in the calendar file on the fly using a simple PHP script. So read on...

Basic layout of embedded VCARD in VCALENDAR file

Just want the source code? Download it here [iphone-contact-download-demo] for a fully working HTML5 Web App that you are free to copy and reproduce or go to http://iphone.mobicontact.info for a working demo. The demo uses the HTML5 cache manifest which downloads the content to your iphone or HTML5 supporting browser for use when offline. Please google for more details about "offline web apps" if you want to know more.

Embedded VCARD in VCALENDAR for iPhone download
You may have read that it is not possible to download contact files (VCARD format data as .vcf file) direct to your iPhone from a web page using Mobile Safari. The browser just does not recognise the .vcf extension and mime type (text/x-vcard) as something it should handle. As an aside, Android and most other mobile devices should be able to handle VCARD files easily enough - the standard itself is as old as the hills!

You may also have read that it IS possible to achieve something workable by requesting the users email address and then email them the contact file OR creating a link to a Google Map entry and extract the contact information from that (Google Map entry requires weeks to attain in the UK).

Whilst these are both viable solutions, they are not what I would call user friendly and I tried, unsuccessfully, to get a client of mine to accept either one of them for their contact download on a mobile web app.

Now at this point I should also mention that you CAN download some third party apps that add support for VCARD (.vcf) files - such as QRAFTER and VCARD GETTER both from Kerem Erkan's excellent QR reader and his blog on the subject and iPad solution, and HIPSCAN vcard importer. But assuming your readers have these apps installed is one step too far in my opinion so I searched for an alternative solution to email, google maps and third party apps.

Before I continue, there are many links describing the problem in more detail:

Stack overflow has several threads on the subject
Forcing vCard download (thesheep.co.uk)
The EMAIL solution and associated blog from the Code Train here vCard options for download
There is also a possible solution using Google Maps here. And this is also covered by this article from Dataplex.
Jonas Schmid talks about serving the file types correctly.
MacRumours thread

Then I got thinking, iPhone DOES support vcalendar files downloaded from a webpage as of IOS5. The VCALENDAR files usually have a .ics extension and are handled by mobile safari bringing up a window where the file can be opened and saved to the calendar. I find it unbelievable that Apple and Mobile Safari support VCAL files but not VCARD files but that is just how it is. So what IF I could attach a VCARD file to a VCALENDAR file?

First obstacle is getting an attachment on a calendar event...

Initially I tried adding an attachment to an Outlook calendar appointment in Windows which whilst possible did not download correctly when linked from a webpage and certainly didn't yield the result I was after. So, I decided to try using Apple software as, after all, we are trying to download onto an iPhone. Using Apple's default calendar application "iCal" presents one fundamental problem - you can't add attachments to an event/calendar appointment! So I googled "adding attachments to calendar events in OSX" or similar and found this excellent article which pointed me in the right direction.

Adding attachments to calendar appointments in OSX.

So a few minutes later and with a copy of BusyCal installed, I was able to create an event and attach a VCARD file to it (previously saved/exported from my contacts in OSX). This step is only necessary to understand the format of the file created when an attachment is added to a calendar item - you DO NOT need to install BusyCal to implement the solution described below but I include it for reference so that you can see how the VCARD is embedded in the VCALENDAR/VEVENT (.ics) file.

The steps I used were on an Apple iMac running OSX Lion:

Export a contact from your Contacts/Address Book to create a VCARD file (.vcf) - you can edit this file with a text editor to strip out all the extra stuff such as UID and PROD-ID if you like.
BEGIN:VCARD
VERSION:3.0
N:Contact;iPhone;;;
FN:iPhone Contact
EMAIL;type=INTERNET;type=WORK;type=pref:[email protected]
TEL;type=CELL;type=VOICE;type=pref:012-345-6789
END:VCARD

Create a new calendar - call it what you like, I used "vcal"- in the "On my Mac" area so that when you export this calendar to generate the .ics file, all you get is the single event with the attached card rather than all the events you might have if you use an existing calendar.
Create a new event - call it anything you like - and give it an arbitrary time and date.
Attach the VCARD file from (1) to this new event - see screenshot.BusyCal on iMac allows attachements to VCALENDAR appointments
Save the event to the calendar.
From the main menu of BusyCal, export the calendar to an .ics file on your local disk - download the zip file here --> Apple calendar event with attached contact file.
You can now use your favourite text editor to examine how Apple store attachments in calendar events and the result is using:ATTACH;VALUE=BINARY;ENCODING=BASE64;FMTTYPE=text/directory;
X-APPLE-FILENAME=iPhone Contact.vcf:
QkVHSU46VkNBUkQNClZFUlNJT046M...etc... [base64 encoded VCARD]
So I then stripped out all the extra stuff I didn't need (trial and error here) until I had the absolute minimum that was still recognised by the iPhone as a valid calendar event with attachment. The reason for doing this is to make the PHP file that creates the calendar event on the final webpage as easy as possible - here is the vcal-minimal of what I reduced it to.

So all of the above, as I said, is not necessary to implement the contact download solution - I just wanted to show you how I got to understanding how Apple attach files using the X-APPLE-FILENAME contact line. And of course, how they encode the VCARD data using base64 encoding. So now we have all the info we need to create VEVENTs on the fly and attach VCARDs to them that can be downloaded direct to an iPhone via Mobile Safari.

1)Upload the contact file (.vcf) you want to be downloaded - you'll need this file so that you can either download it direct to non-iphones or base64 encode it for the iPhone.

2)Create a link to a PHP file that will generate the calendar event on the fly such as:
Download Contact iPhone perhaps in a HTML5 mobile app like below:
Simple link to the PHP file to download the contact within VCALENDAR

3)Create or upload the vcal.php with the code here iphonecontact-source-code. This PHP file applies the correct header/content type for the calendar file to be downloaded then you have a choice of either getting the calendar file contents directly "iphonecontact.ics" as done in "vcal-from-file.php" or generating the calendar on the fly as shown in "vcal.php". The latter is my preferred method because you get a nice timestamped calendar event showing the time and date of the download.

4)That's it! You're all set - now browse to the web page on your iphone and click the link to execute "vcal.php". Your browser should now show the iphonecontact.ics file and ask you to open it in the phones Calendar application
Calendar appointment downloaded from web page

5)Select "Open in..." and you will be presented with the calendar appointment and the attached contact file.
Calendar event with attached contact file

6)Notice how I set the title of the calendar event to something useful to tell the user what to do with the embedded contact file (you can see the line in vcal.php that sets the SUMMARY field for the event). So now click the attached contact file...
Opened contact file

7)And then "Create New Contact" and you're nearly there...
Contact SAVED!!!

Save the contact and curse under your breath at the hoops Apple made you jump through!

Now there are a couple of points I'd like to mention here based on my experiences using this technique on HTML5 web apps:

Using the cache manifest - I had some unexpected behaviour/problems serving the the calendar file (iphonecontact.ics) if it had been cached - I just couldn't get it to work so I exclude it from the manifest which means it is always downloaded - source code is included in this zip file iphonecontact-source-code.

You could of course do some user agent sniffing to detect in the "vcal.php" PHP file whether to serve the VCALENDAR file to an iPhone and the VCARD file itself to all other browsers. Its a simple enough check and well documented via a google search so I'll leave it to you to figure that out if you need to and feel free to post the code here if you do.

I think that's about it - so to summarise:

Mobile safari doesn't support VCARD (.vcf) files directly but does support VCALENDAR (.ics) files.

  • Current best solutions are to email the contact by requesting the users email address OR to embed the contact in a google map link OR download an app that handles VCARDS.
  • Apple does support attachments to calendar files but not easily so once we know how this is done we can do it in PHP.
  • Embed a VCARD into a VCALENDAR file to allow a user to save a contact to their address book with just an extra click or two.

I hope you like this solution - it is as good as I think we are going to get until Apple relent and allow Mobile Safari to accept VCARD files.

Until next time...

倥絔 2024-08-21 16:41:44

Apple 使用:

ATTACH;VALUE=BINARY;ENCODING=BASE64;FMTTYPE=text/directory;
X-APPLE-FILENAME=iPhone Contact.vcf:
QkVHSU46VkNBUkQNClZFUlNJT046M…etc… [base64 encoded VCARD]

用于在 VCALENDAR 文件中嵌入 VCARD。创建一个 VCALENDAR 文件,然后在其中对您的 VCARD 进行 Base64 编码 - 下面的代码片段(完整详细信息在我的博客上)

<?php
# Send correct headers      
header("Content-type: text/x-vcalendar; charset=utf-8"); 
# Alternatively: application/octet-stream
# Depending on the desired browser behaviour
# Be sure to test thoroughly cross-browser

header("Content-Disposition: attachment; filename=\"iphonecontact.ics\";");
# Output file contents - simple version
#echo file_get_contents("iphonecontact.ics");

# Generate file contents - advanced version
# BEGIN:VCALENDAR
# VERSION:2.0
# BEGIN:VEVENT
# DTSTART;TZID=Europe/London:20120617T090000
# DTEND;TZID=Europe/London:20120617T100000
# SUMMARY:iPhone Contact
# DTSTAMP:20120617T080516Z
# ATTACH;VALUE=BINARY;ENCODING=BASE64;FMTTYPE=text/directory;
#  X-APPLE-FILENAME=iphonecontact.vcf:
#  QkVHSU46VkNBUkQNClZFUlNJT046My4wDQpOOkNvbnRhY3Q7aVBob25lOzs7DQpGTjppUGhvbm
#  UgQ29udGFjdA0KRU1BSUw7VFlQRT1JTlRFUk5FVDtUWVBFPVdPUks6aXBob25lQHRoZXNpbGlj
#  b25nbG9iZS5jb20NClRFTDtUWVBFPUNFTEw7VFlQRT1WT0lDRTtUWVBFPXByZWY6KzQ0MTIzND
#  U2Nzg5MA0KRU5EOlZDQVJE
# END:VEVENT
# END:VCALENDAR

echo "BEGIN:VCALENDAR\n";
echo "VERSION:2.0\n";
echo "BEGIN:VEVENT\n";
echo "SUMMARY:Click attached contact below to save to your contacts\n";
$dtstart = date("Ymd")."T".date("Hi")."00";
echo "DTSTART;TZID=Europe/London:".$dtstart."\n";
$dtend = date("Ymd")."T".date("Hi")."01";
echo "DTEND;TZID=Europe/London:".$dtend."\n";
echo "DTSTAMP:".$dtstart."Z\n";
echo "ATTACH;VALUE=BINARY;ENCODING=BASE64;FMTTYPE=text/directory;\n";
echo " X-APPLE-FILENAME=iphonecontact.vcf:\n";
$vcard = file_get_contents("iphonecontact.vcf");        # read the file into memory
$b64vcard = base64_encode($vcard);                      # base64 encode it so that it can be used as an attachemnt to the "dummy" calendar appointment
$b64mline = chunk_split($b64vcard,74,"\n");             # chunk the single long line of b64 text in accordance with RFC2045 (and the exact line length determined from the original .ics file exported from Apple calendar
$b64final = preg_replace('/(.+)/', ' $1', $b64mline);   # need to indent all the lines by 1 space for the iphone (yes really?!!)
echo $b64final;                                         # output the correctly formatted encoded text
echo "END:VEVENT\n";
echo "END:VCALENDAR\n";
?>

Apple uses:

ATTACH;VALUE=BINARY;ENCODING=BASE64;FMTTYPE=text/directory;
X-APPLE-FILENAME=iPhone Contact.vcf:
QkVHSU46VkNBUkQNClZFUlNJT046M…etc… [base64 encoded VCARD]

for embedded VCARD in VCALENDAR files. Create a VCALENDAR file and then base64 encode your VCARD within it - code snippet below (full details on my blog)

<?php
# Send correct headers      
header("Content-type: text/x-vcalendar; charset=utf-8"); 
# Alternatively: application/octet-stream
# Depending on the desired browser behaviour
# Be sure to test thoroughly cross-browser

header("Content-Disposition: attachment; filename=\"iphonecontact.ics\";");
# Output file contents - simple version
#echo file_get_contents("iphonecontact.ics");

# Generate file contents - advanced version
# BEGIN:VCALENDAR
# VERSION:2.0
# BEGIN:VEVENT
# DTSTART;TZID=Europe/London:20120617T090000
# DTEND;TZID=Europe/London:20120617T100000
# SUMMARY:iPhone Contact
# DTSTAMP:20120617T080516Z
# ATTACH;VALUE=BINARY;ENCODING=BASE64;FMTTYPE=text/directory;
#  X-APPLE-FILENAME=iphonecontact.vcf:
#  QkVHSU46VkNBUkQNClZFUlNJT046My4wDQpOOkNvbnRhY3Q7aVBob25lOzs7DQpGTjppUGhvbm
#  UgQ29udGFjdA0KRU1BSUw7VFlQRT1JTlRFUk5FVDtUWVBFPVdPUks6aXBob25lQHRoZXNpbGlj
#  b25nbG9iZS5jb20NClRFTDtUWVBFPUNFTEw7VFlQRT1WT0lDRTtUWVBFPXByZWY6KzQ0MTIzND
#  U2Nzg5MA0KRU5EOlZDQVJE
# END:VEVENT
# END:VCALENDAR

echo "BEGIN:VCALENDAR\n";
echo "VERSION:2.0\n";
echo "BEGIN:VEVENT\n";
echo "SUMMARY:Click attached contact below to save to your contacts\n";
$dtstart = date("Ymd")."T".date("Hi")."00";
echo "DTSTART;TZID=Europe/London:".$dtstart."\n";
$dtend = date("Ymd")."T".date("Hi")."01";
echo "DTEND;TZID=Europe/London:".$dtend."\n";
echo "DTSTAMP:".$dtstart."Z\n";
echo "ATTACH;VALUE=BINARY;ENCODING=BASE64;FMTTYPE=text/directory;\n";
echo " X-APPLE-FILENAME=iphonecontact.vcf:\n";
$vcard = file_get_contents("iphonecontact.vcf");        # read the file into memory
$b64vcard = base64_encode($vcard);                      # base64 encode it so that it can be used as an attachemnt to the "dummy" calendar appointment
$b64mline = chunk_split($b64vcard,74,"\n");             # chunk the single long line of b64 text in accordance with RFC2045 (and the exact line length determined from the original .ics file exported from Apple calendar
$b64final = preg_replace('/(.+)/', ' $1', $b64mline);   # need to indent all the lines by 1 space for the iphone (yes really?!!)
echo $b64final;                                         # output the correctly formatted encoded text
echo "END:VEVENT\n";
echo "END:VCALENDAR\n";
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文