I have been using the Google Calendar API for a number of years with great success. I use requests from php using curl.
I've been trying to achieve the same with the contacts api. At the moment all I want to do is retrieve a list of contacts as json for a user I've retrieved a token for through OAuth2. I'm using the simple code below for testing.
$accessToken = "thebiglongaccesstoken1234";
$userMail = "[email protected]";
$requestURL ="https://www.google.com/m8/feeds/contacts/".$userMail."/full?v=3.0&alt=json";
$headers = array("Authorization: OAuth ".$accessToken);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $requestURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$responseArray = json_decode($response, TRUE);
var_dump($responseArray);
print '<br/>Response: <pre>' . print_r($responseArray, true) . '</pre>';
print 'curl Error: <pre>' . print_r(curl_error($ch), true) . '</pre>';
the response in the browser is:
NULL Response: curl Error:
I can get this request to work in the google OAuth 2.0 playground.
I don't want to use a library because all I eventually want to do is find a users contact by email address to get the phone number. I have made my own library of functions for the calendar so don't want to add extra unused stuff. It should only require a few lines of code.
I think I've just stared at it too long now and am missing the obvious. Any help appreciated.
UPDATE:
I got this out using curl_getinfo()
Array ( [url] => https://www.google.com/m8/feeds/contacts/[email protected]/full?v=3.0&alt=json [content_type] => text/html; charset=utf-8 [http_code] => 401 [header_size] => 457 [request_size] => 208 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.1244 [namelookup_time] => 0.000837 [connect_time] => 0.00306 [pretransfer_time] => 0.015428 [size_upload] => 0 [size_download] => 11875 [speed_download] => 95458 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => 0 [starttransfer_time] => 0.123992 [redirect_time] => 0 [certinfo] => Array ( )
)
json_decode
? The Contacts API generally gives plain-text errors. Also a response status code would be helpful too. – Blake O'Hare