jsonSerialize

30 views
Skip to first unread message

Gus

unread,
Jun 7, 2016, 10:46:22 PM6/7/16
to SabreDAV Discussion
What am I missing in trying to get json output from ical feed:

<?php
use Sabre\VObject;

include 'vendor/autoload.php';

$mycal = array VObject\Node::jsonSerialize('http://theicalfeed.ics');

echo $mycal;
?>

Thanks

Evert Pot

unread,
Jun 7, 2016, 11:04:54 PM6/7/16
to sabredav...@googlegroups.com


On 2016-06-07 10:46 PM, Gus wrote:
> What am I missing in trying to get json output from ical feed:

You're missing quite a bit.

>
> <?php
> use Sabre\VObject;
>
> include 'vendor/autoload.php';
>
> $mycal = array VObject\Node::jsonSerialize('http://theicalfeed.ics');
>
> echo $mycal;
> ?>


Try:

$source = file_get_contents('http://hostname/theicalfee.ics');
$vobj = VObject\Reader::read($source);
$json = json_encode($vobj);

echo $json;

Gus

unread,
Jun 8, 2016, 7:12:28 AM6/8/16
to SabreDAV Discussion, m...@evertpot.com
Yes that worked! Thank you...
However, having some difficulty as I am trying to convert to a 2-dimentional json by pulling 'summary' and 'dtstamp' only

~~~
$source = file_get_contents('http://hostname/theicalfee.ics'); 
$vobj = VObject\Reader::read($source); 
$json = json_encode($vobj); 
$contdecode = json_decode($json);

$item=array(); 
foreach ( $contdecode['vevent'] as $value ) {
    $result['Date'] = date("l - M jS",strtotime($value['dtstart'])) ; 
$result['Schedule'] = $value['summary'];
array_push($item,$result);
}

echo json_encode($item,JSON_PRETTY_PRINT);  

~~~~~~

Evert Pot

unread,
Jun 8, 2016, 10:59:53 AM6/8/16
to sabredav...@googlegroups.com


On 2016-06-08 7:12 AM, Gus wrote:
> Yes that worked! Thank you...
> However, having some difficulty as I am trying to convert to a
> 2-dimentional json by pulling 'summary' and 'dtstamp' only
>
> ~~~
> $source = file_get_contents('http://hostname/theicalfee.ics'
> <http://hostname/theicalfee.ics'>);
> $vobj = VObject\Reader::read($source);
> $json = json_encode($vobj);
> $contdecode = json_decode($json);
>
> $item=array();
> foreach ( $contdecode['vevent'] as $value ) {
> $result['Date'] = date("l - M jS",strtotime($value['dtstart'])) ;
> $result['Schedule'] = $value['summary'];
> array_push($item,$result);
> }
>
> echo json_encode($item,JSON_PRETTY_PRINT);

Turning it into json and back doesn't make a lot of sense. The whole
point of vobject is that it helps you read iCalendar. Plus, you're
making a lot of assumptions about how the json structure actually looks
like. You should consider taking a look at the actual output of your
original json_encode.

Regardless, you don't need these two lines:

> $json = json_encode($vobj);
> $contdecode = json_decode($json);

Just work with $vobj directly. There's a whole bunch of documentation on
the site:

http://sabre.io/vobject/icalendar/

Evert

Gus

unread,
Jun 8, 2016, 1:06:55 PM6/8/16
to SabreDAV Discussion, m...@evertpot.com
This did it for me...Thanks for the help..
I hope it can be of use to other people:

$source = file_get_contents('http://xyz.com/ical.ics'); 
$vobj = VObject\Reader::read($source); 

$item=array();
foreach($vobj->VEVENT as $data) {
$eventdate = $data->DTSTART->getDateTime()->format(\DateTime::W3C);;
$result['Date'] = date("l - M jS",strtotime($eventdate)) ; 
$result['Schedule'] = (string)$data->SUMMARY;;
array_push($item,$result);
}
echo json_encode($item,JSON_PRETTY_PRINT); 

Gus 

Evert Pot

unread,
Jun 8, 2016, 1:08:56 PM6/8/16
to Gus, SabreDAV Discussion
Great!

Here's a small modification to make it a bit simpler even:

On 2016-06-08 01:06 PM, Gus wrote:
> This did it for me...Thanks for the help..
> I hope it can be of use to other people:
>
> $source = file_get_contents('http://xyz.com/ical.ics');
> $vobj = VObject\Reader::read($source);
>
> $item=array();
> foreach($vobj->VEVENT as $data) {
> $eventdate = $data->DTSTART->getDateTime()->format(\DateTime::W3C);;
> $result['Date'] = date("l - M jS",strtotime($eventdate));

Instead of those last two lines, you can just do:

$result['Date'] = $data->DTSTART->getDateTime()->format('l - M jS');
> http://sabre.io/vobject/icalendar/ <http://sabre.io/vobject/icalendar/>
>
> Evert
>

Gus

unread,
Jun 8, 2016, 1:42:44 PM6/8/16
to SabreDAV Discussion, saf...@gmail.com, m...@evertpot.com
Thanks again.

I also added:
$vobj = VObject\Reader::read($source);
$newVobj = $vobj->expand(new DateTime('2016-06-01'), new DateTime('2017-12-31')); 

as the ical has lots of recurring events.. I am hoping that it will also handle the recurring event exceptions ( have yet to check that!)

Gus
Reply all
Reply to author
Forward
0 new messages