Not getting Google analytics data in PHP
<p>I am trying to get analytics data can someone help me, below are the ways I tried but I am getting 500 internal server.</p>

<pre><code>&lt;?php
require_once realpath(dirname(__FILE__) . '/google-api-php-client-master/src/Google/autoload.php');
session_start();   
$client = new Google_Client();
$client-&gt;setApplicationName("meme");
$client-&gt;setDeveloperKey('my app key here');
$service = new Google_Service_Analytics($client);
$projectId = '103785255';
$from = '2016-01-18'; // 2 days
$to = date('Y-m-d'); // today
$metrics = 'ga:pageviews';
$dimensions = 'ga:date,ga:year,ga:month,ga:day';
$data = $service-&gt;data_ga-&gt;get('ga:'.$projectId, $from, $to, $metrics, array('dimensions' =&gt; $dimensions));
print_r($data);
?&gt;
</code></pre>

<p>Second method with oauth-2.0 authentication</p>

<pre><code>&lt;?php
require_once realpath(dirname(__FILE__) . '/google-api-php-client-master/src/Google/autoload.php');
session_start();
$client_id = 'my client id .apps.googleusercontent.com';
$client_secret = 'my client secret';
$redirect_uri = 'redirect uri';

$client = new Google_Client();
$client-&gt;setApplicationName("meme");
$client-&gt;setClientId($client_id);
$client-&gt;setClientSecret($client_secret);
$client-&gt;addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);
$client-&gt;setRedirectUri($redirect_uri);
$client-&gt;setAccessType("offline");

if (isset($_GET['code'])) {
$client-&gt;authenticate($_GET['code']);print_r($client);exit;
$_SESSION['token'] = $client-&gt;getAccessToken();
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

if (!$client-&gt;getAccessToken() &amp;&amp; !$_SESSION['token']) {
    $authUrl = $client-&gt;createAuthUrl();
    print "&lt;div&gt;&lt;a class='login' href='$authUrl'&gt;Connect Me!&lt;/a&gt;&lt;/div&gt;";
}

if($_SESSION['token']){
  $client-&gt;setAccessToken($_SESSION['token']);
  $service = new Google_Service_Analytics($client);
  $projectId = '103785255';
  $_params[] = 'pageviews';
  $from = '2016-01-18'; // 2 days
  $to = date('Y-m-d'); // today

    $metrics = 'ga:pageviews';

    $dimensions = 'ga:date,ga:year,ga:month,ga:day';
    $data = $service-&gt;data_ga-&gt;get('ga:'.$projectId, $from, $to, $metrics, array('dimensions' =&gt; $dimensions));
    foreach($data['totalsForAllResults'] as $row) {
       echo $row;
    }   
}
?&gt;
</code></pre>

<p>If you see I have placed a print statement after $client->authenticate($_GET['code']); where I am getting 500 internal server error If I print the code before authenticate method it is printing, facing issue while calling $client->authenticate($_GET['code']).  Even followed the below links and endup with the same issues</p>

<p><a href="https://stackoverflow.com/questions/25224300/google-php-client-wont-authenticate-code-invalid-request">Google PHP client won&#39;t authenticate code, invalid_request</a></p>

<p><a href="http://www.daimto.com/google-oauth2-php/" rel="nofollow noreferrer">http://www.daimto.com/google-oauth2-php/</a></p>