Reputation: 433
First and foremost - I am not a PHP developer. I concentrate on front-end but am wanting to expand my knowledge/play around with some personal projects to further my understanding. Therefore some of my terminology and fundamental understand of what I'm trying to achieve may be a little ... 'off'.
I have followed all the steps provided by Google and have managed to get to a point with the Google Calendar API where I can list calendar events using a simple var_dump()
My problem lies in trying to make this data useful. I want to do this with JSON. I am aware of the json_encode()
function but am not sure how I should be using it within the context of what I am trying to achieve (that is to have a JSON file which can hook into the data I need to ultimately display a calendar on the front-end). I am aware that json_encode()
respects property visibility/scope but this is my problem - I cannot figure out how to ensure json_encode()
will also encode the protected properties/methods in the object I am returned.
Here is my code:
$eventsArgs = array(
'maxResults' => 10,
'singleEvents' => true,
);
$events = $calendarService->events->listEvents('***@gmail.com', $eventsArgs);
$listEvents = fopen('list_events.json', 'w');
foreach($events->getItems() as $event) {
fwrite($listEvents, json_encode($event, JSON_PRETTY_PRINT));
}
fclose($listEvents);
I am wanting to pull in protected properties/methods such as start
and end
as displayed here with a simple var_dump()
...
["modelData":protected]=> array(5) {
["creator"]=>
array(3) {
["email"]=>
string(23) "***@gmail.com"
["displayName"]=>
string(14) "***"
["self"]=>
bool(true)
}
["organizer"]=>
array(3) {
["email"]=>
string(23) "***@gmail.com"
["displayName"]=>
string(14) "***"
["self"]=>
bool(true)
}
["start"]=>
array(1) {
["dateTime"]=>
string(25) "2014-04-27T05:00:00+01:00"
}
["end"]=>
array(1) {
["dateTime"]=>
string(25) "2014-04-27T06:00:00+01:00"
}
I am really trying to educate myself here and unfortunately have hit a wall and the answers I find, if I'm honest, are a little too technical for my understanding. Technical as well as non-technical explanations welcome!
Thanks for reading.
Upvotes: 3
Views: 946
Reputation: 31
I had the exact same problem as you! I finally found:
$json = json_encode( (array)$events);
Answer is from another post:Here
Please Note:
It produces Unicode NUL characters: \u0000*\u0000 on the outer array keys. But the full structure with the start and end values remains. If you are interested in removing the Nul chars you can use str_replace() or explode. Understanding what \u0000 is in PHP / JSON and getting rid of it
Terminus's answer above is great but using this was preferable for my situation because I needed to match a different json file from google node.js api .
Upvotes: 2
Reputation:
So, the problem, as you pointed out, is that properties you want to access are protected and json_encode
respects that and will not access them. Read up on public, private, and protected. Good stuff
Because the values are protected, you'll have to pull the data you want out of $event
and into an unprotected array and then json_encode
that. Untested code follows:
$eventsArgs = array(
'maxResults' => 10,
'singleEvents' => true,
);
$events = $calendarService->events->listEvents('***@gmail.com', $eventsArgs);
$listEvents = fopen('list_events.json', 'w');
foreach($events->getItems() as $event) {
$jsonArr = array(
"start"=>$event->start->dateTime
,"end"=>$event->end->dateTime
);
fwrite($listEvents, json_encode($jsonArr, JSON_PRETTY_PRINT));
}
fclose($listEvents);
Now even that code might not work. You may have to find the appropriate method (that's 'method' as in: function belonging to a class) to get the properties you want out of a Google Calendar Event, and then put them in an array and than json_encode it.
So the 'getter' method might be: $event->get('start')->dateTime
or something. I have no experience with the Google Calendar API, so can't really help you in that regard
Lastly, know that you can have the PHP script echo
the json_encode
d results so you won't need the file. (of course, I have no idea what all of your code looks like, so that could be a bad idea)
foreach(...) {
echo json_encode($yourArray);
}
Upvotes: 2