Can't connect to the Google Analytics APICan't connect to the Google Analytics API

Posted April 29th, 2009 in PHP (Updated April 29th, 2009)

I had an email this morning about my Google Analytics API PHP class which said "I'm trying to get your google API class working in my application, it's failing at logon at the moment" so I had a look at the possible reasons for failure and present these here.

CURL is not installed

It's possible CURL is not installed with your PHP install. You can check this by running the following code:

if(function_exists("curl_init")) {
    echo "curl is installed";
}
else {
    echo "curl is not installed";
}

The other way is to use phpinfo() and look through the page for the curl section to see if it's installed.

Outbound connections on port 443 are firewalled

The connections to the Google Analytics API are to https://www.google.com/accounts/ClientLogin which requires an outbound connection on port 443. It could be possible that outbound connections on port 443 are firewalled.

Also, although I'm not sure about this or how to check for it, it's possible that SSL itself may need to be enabled in PHP to be able to connect on port 443 with CURL. But don't quote me on this one...

The login name and password are wrong

It's possible the login name and password are wrong. The login name needs to be in the form of an email address e.g. me@example.com and needs to be the one used to log into Google Analytics. If you can log into the web based interface (http://www.google.com/analytics/ with a web browser) with the login/email and password it should also work in this class.

My code is wrong

I haven't had any issues connecting myself, other than when I purposely used an incorrect login/password combination to test it out. However it's quite possible that my code is wrong and won't always work as intended.

In particular the following line could be getting the authorisation token incorrectly from the response data.

preg_match('/Auth=(.*)/', $output, $matches);

A bit of debugging in that section of code would be required by echo'ing out the actual response (the $output bit) and also the $info['http_code'] to see what Google has returned in response.

I may modify the login function later on today and repost the class with a debugging flag for that function to output what's going on behind the scenes.

Other errors and success stories

I don't have a comments function yet on this blog, otherwise I'd invite you to add your comments about other possible errors that may occur and also any success stories you've had. So instead, for the moment, please fire these through to me in an email and I can add notes to this post and/or to a new one.

Update later on in the day

The guy who emailed me about the issue he was having discovered the following needed to be added in order for the CURL stuff to work on a Windows installation of PHP:

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

Thanks for Kyle from www.e-strategy.net - I'll update my class in the morning to include these options and also some additional debugging output information.

Related posts:

Comments

blog comments powered by Disqus