I'm currently working on a form in PHP which would consolidate two different tasks: booking a space for an event, and registering the event for an online calendar. I have the Google Calendar piece nailed down with the API Client for PHP, but the Calendar Resources are still proving to be a problem.
Here is some information as to where I currently am:
- I am using the general API to handle the OAuth2 authorization on the Calendar and Resource scopes.
- Resource Scope: https://apps-apis.google.com/a/feeds/calendar/resource/
- I use the access token to make a cURL GET request to the Resource feed in an attempt to retrieve the XML data on all of the available resources.
- Resource Call: https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/
- Assuming the above worked, I would use the resource emails pulled from the XML to invite selected resources to the event made with the Calendar API.
My current dilemma involves the data I have returned from the call. When I use Google OAuth2 Playground to make the request with the above URIs, it retrieves the XML data without an issue. When I make the same attempt in PHP, I get the data in a single string with the following format:
- https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/5798411496212015-02-12T18:42:23.268Z
- "request_uri"."resource_id"."date_of_access" (x 100)
I know that each request will only return 100 resources per request, and both the playground and my php return what appear to be 100 entries, so it does not appear to necessarily be an authorization problem. That being said, I'm not sure what I would be doing differently than the playground.
My cURL request is the following (once retrieving the access_token):
$url = 'https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/';
$token = json_decode($_SESSION['access_token'], true);
$access_token = $token['access_token'];
$token_type = $token['token_type'];
//open connection
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => array('Authorization: '.$token_type.' '.$access_token,),
));
// $output contains the returned data
$output = curl_exec($ch);
if($output === false) { echo 'Curl error: ' . curl_error($ch); }
else { echo $output; }
// close curl resource to free up system resources
curl_close($ch);
Is there something, a cURL setting or a header, that I am missing? I can't guess what it would be, especially when the Playground's request is just the following:
GET /a/feeds/calendar/resource/2.0/domain/ HTTP/1.1
Host: apps-apis.google.com
Content-length: 0
Authorization: Bearer access_token
UPDATE
Hans Z.'s answer prompted me (thank you!) to search for ways to put the XML data into an array, since I need to use the data obtained rather than display it. (My apologies for not making that clear initially.) There are a lot of posts on the subject, and after putting the output through SimpleXML, I retrieve the following as an entry for each resource (x100):
[0] => SimpleXMLElement Object
(
[id] => https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/579841149621
[updated] => 2015-02-18T17:04:16.481Z
[link] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[rel] => self
[type] => application/atom+xml
[href] => https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/579841149621
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[rel] => edit
[type] => header('Content-type: application/xml');
[href] => https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/579841149621
)
)
)
)
As seen above, this does not provide any information about the resource itself, such as it's name or email (or id, in any direct parse-less way). I tried running the XML output from the playground through SimpleXML and am reviving the exact same output. Assuming it to be something with SimpleXML, I did some more searches and came across this question, this question, and this blog post. I myself have been poking around with what they suggest, but with little success thus far. If anyone would like to figure it out before me (as I have to attend meetings for most of the rest of this day), here is an example of the XML structure:
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:apps='http://schemas.google.com/apps/2006'>
<id>https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain</id>
<updated>2015-02-18T17:03:03.057Z</updated>
<link rel='next' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/?start=579841149621'/>
<link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain'/>
<link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain'/>
<link rel='self' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain'/>
<openSearch:startIndex>1</openSearch:startIndex>
<entry>
<id>https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/579841149621</id>
<updated>2015-02-18T17:03:03.056Z</updated>
<link rel='self' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/579841149621'/>
<link rel='edit' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/579841149621'/>
<apps:property name='resourceId' value='579841149621'/>
<apps:property name='resourceCommonName' value='Projector'/>
<apps:property name='resourceEmail' value='domain_key@resource.calendar.google.com'/>
</entry>
//99 more times:
<entry>
...
</entry>