{"id":2695,"date":"2016-06-21T12:00:40","date_gmt":"2016-06-21T11:00:40","guid":{"rendered":"https:\/\/www.bronco.co.uk\/our-ideas\/?p=2695"},"modified":"2020-07-02T11:44:09","modified_gmt":"2020-07-02T10:44:09","slug":"integrating-ical","status":"publish","type":"post","link":"https:\/\/www.bronco.co.uk\/our-ideas\/integrating-ical\/","title":{"rendered":"Integrating iCal in PHP"},"content":{"rendered":"\n<p>iCal is a file format which allows the sharing of generic calendar information. A lot of sites such as HomeAway and AirBnB generate iCal files so you can synchronise bookings across platforms on which your properties may be advertised.<\/p>\n\n\n\n<p>If one of these platforms is your own website, you may need to read the external files (from HomeAway etc) and enter the information into your database to keep it up to date. Reading and\/or writing these files is actually pretty straightforward &#8211; you just need to know the correct format.<\/p>\n\n\n\n<p>First of all, let&#8217;s take a look at what a basic iCal file looks like:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plane\"><code>BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-\/\/UniqueID, Inc.\/\/EN\nBEGIN:VEVENT\nUID:3093bdc35333dafc24a4e9ffad352f6f\nDTSTAMP:20160615T110419Z\nDTSTART:20170811\nDTEND:20170818\nSUMMARY:Booking for John Smith\nEND:VEVENT\nBEGIN:VEVENT\nUID:6c5e65585905ee04d06df38aa2245319\nDTSTAMP:20160615T110419Z\nDTSTART:20170804\nDTEND:20170811\nSUMMARY:Booking for Joe Bloggs\nEND:VEVENT\nEND:VCALENDAR<\/code><\/pre><\/div>\n\n\n\n<p>(A handy guide to the specification of each attribute is here <a href=\"http:\/\/www.kanzaki.com\/docs\/ical\/\">http:\/\/www.kanzaki.com\/docs\/ical\/<\/a>)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Writing<\/h2>\n\n\n\n<p>To create this, simply loop through the data you wish to include (i.e. each of the bookings for your property) and build a string to iCal specification. Don&#8217;t forget to set the correct headers before you echo out the string; these tell the program reading your file that it is in iCal format.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>$ical = &quot;BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-\/\/[YOUR WEBSITE], Inc.\/\/EN&quot;;\n\n$query = mysqli_query(&quot;SELECT * FROM booking_dates&quot;);\nwhile($row = mysqli_fetch_object($query)){\n    $ical .= &quot;\nBEGIN:VEVENT\nUID:&quot; .md5(uniqid(mt_rand(), true))\nDTSTAMP:&quot;.gmdate(&quot;Ymd&quot;).&quot;T&quot;.gmdate(&quot;His&quot;).&quot;Z\nDTSTART:&quot;.date(&quot;Ymd&quot;, strtotime($row-&gt;start_date)).&quot;\nDTEND:&quot;.date(&quot;Ymd&quot;, strtotime($row-&gt;end_date)).&quot;\nSUMMARY:Booking for $row-&gt;name\nEND:VEVENT&quot;;\n}\n\n$ical .= &quot;\nEND:VCALENDAR&quot;;\n\nheader(&quot;Content-type: text\/calendar; charset=utf-8&quot;);\nheader(&quot;Content-Disposition: inline; filename=calendar.ics&quot;);\necho $ical;<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Reading<\/h2>\n\n\n\n<p>A very useful function I found (thanks Steffen Behn <a href=\"http:\/\/stackoverflow.com\/a\/13844260\">http:\/\/stackoverflow.com\/a\/13844260<\/a>) simplifies this immensely.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>function icsToArray($paramUrl){\n    $icsFile = file_get_contents($paramUrl);\n\n    $icsData = explode(&quot;BEGIN:&quot;, $icsFile);\n\n    foreach($icsData as $key =&gt; $value){\n        $icsDatesMeta[$key] = explode(&quot;\\n&quot;, $value);\n    }\n\n    foreach($icsDatesMeta as $key =&gt; $value){\n        foreach($value as $subKey =&gt; $subValue){\n            if($subValue != &quot;&quot;){\n                if($key != 0 && $subKey == 0){\n                    $icsDates[$key][&quot;BEGIN&quot;] = trim($subValue);\n                }else{\n                    $subValueArr = explode(&quot;:&quot;, $subValue, 2);\n                    $icsDates[$key][$subValueArr[0]] = trim($subValueArr[1]);\n                }\n            }\n        }\n    }\n\n    return $icsDates;\n}<\/code><\/pre><\/div>\n\n\n\n<p>Use the above function as such:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>$icspath = &quot;http:\/\/path-to-file.ics&quot;;\n$ics = icsToArray($icspath);\nforeach($ics as $a){\n    if($a[&#39;BEGIN&#39;] == &quot;VEVENT&quot;){\n        $start_date = date(&quot;Y-m-d&quot;, strtotime($a[&#39;DTSTART&#39;]));\n        $end_date = date(&quot;Y-m-d&quot;, strtotime($a[&#39;DTEND&#39;]));\n        $summary = $a[&#39;SUMMARY&#39;];\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<p>Then you can do with the data as you wish. In the above example I&#8217;ve converted the dates to standard &#8220;Y-m-d&#8221; format and assigned the various data to variables. You would then most likely input this into your database in order to keep your calendar up to date with the source of the ics file.<\/p>\n\n\n\n<p>And that&#8217;s it!<\/p>\n\n\n\n<p>This code is free to use at your own discretion. It comes without warranty. Please feel free to feedback any edits.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>iCal is a file format which allows the sharing of generic calendar information. A lot of sites such as HomeAway and AirBnB generate iCal files so you can synchronise bookings across platforms on which your properties may be advertised. If one of these platforms is your own website, you may need to read the external [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":2705,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"class_list":["post-2695","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-and-ux"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/2695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/comments?post=2695"}],"version-history":[{"count":0,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/2695\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/media\/2705"}],"wp:attachment":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/media?parent=2695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/categories?post=2695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}