I am working on a dynamic website, which relies heavily on ajax calls. I've noticed that when I load in my front page, and try to access the main search box on this page, it takes 5-7 seconds until the ajax request completes which populates the main search box. I've managed to figure out that delay is being caused by the Google API PHP Library, which starts as soon as the HTML of my page is loaded, to try to authenticate the visiting user. I've benchmarked the script in parts, and figured out, that the problem is in the "verifyIdToken" method of the library, which to complete, takes around 5 seconds.
I've noticed that in the documentation and tutorials, Google says: "The library will automatically download and cache the certificate required for verification, and refresh it if it has expired."
Does anyone know if:
- this is the normal behaviour of this method, and it SHOULD take this long to execute?
- the caching is the standard behaviour, or do I have to set it somewhere to actually cache the required data?
- if it is the standard behaviour, how could I check if the caching actually happens or not?
Here is the full code I am working on, maybe someone spots an error in it:
$client_id='xxxxxx';
$client_secret='xxxxxxxx';
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setApplicationName("Converser Google API");
$client->addScope('https://www.googleapis.com/auth/plus.me');
$client->addScope('https://www.google.com/m8/feeds');
$client->addScope('https://mail.google.com/');
$client->addScope('email');
$client->setRedirectUri('postmessage');
$client->setAccessType("offline");
$client->setApprovalPrompt("force");
if (!isset($_SESSION['google-api']['access_token'])) {
if (isset($_REQUEST['code'])) g_exchangeCode($_REQUEST['code']);
else g_kill(0);
} else {
$accessToken=$_SESSION['google-api']['access_token'];
$client->setAccessToken($accessToken);
$returnData['auth_by']='token';
}
if($client->isAccessTokenExpired()) {
if (isset($_REQUEST['gid']) && !empty($_REQUEST['gid'])) g_refreshToken($_REQUEST['gid']);
else if (isset($_REQUEST['code']) && !empty($_REQUEST['code'])) g_exchangeCode($_REQUEST['code']);
else g_kill(1);
}
$token_data = $client->verifyIdToken()->getAttributes();
It's caused by ipv6, you need to use ipv4 or you get this error
.
This commit allowed cURL options to be set through
setClassConfig
. Setting this should solve it.'CURLOPT_IPRESOLVE', 'CURL_IPRESOLVE_V4'
If you are using master branch than checkout this.