So, I was wondering if the following code could actually be ported to laravel?
<?php
require_once 'data.php';
require_once 'lib/google-api-php-client-master/src/Google/autoload.php';
session_start();
$client_id = 'CLIENT_ID_HERE';
$client_secret = 'CLIENT_SECRET_HERE';
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$simple_api_key = 'API_KEY_HERE';
$client = new Google_Client();
$client->setApplicationName("APPLICATION_NAME");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/analytics.readonly");
$client->setApprovalPrompt('force');
$client->setAccessType('offline');
if(isset($_SESSION['ga_token_time']) && (time() - $_SESSION['ga_token_time'] > 60)){
unset($_SESSION['ga_token']);
unset($_SESSION['ga_token_time']);
}
$result = mysqli_query($con,'SELECT token FROM user WHERE user_id='.$_SESSION['user_id']);
$row = $result->fetch_assoc();
if($row['token'] != '') {
$client->refreshToken($row['token']);
$_SESSION['ga_token'] = $client->getAccessToken();
$_SESSION['ga_token_time'] = time();
} elseif (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['ga_token'] = $client->getAccessToken();
$_SESSION['ga_token_time'] = time();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['ga_token']) && trim($_SESSION['ga_token']) !== '') {
$client->setAccessToken($_SESSION['ga_token']);
$token = json_decode($_SESSION['ga_token']);
if(isset($token->refresh_token)) {
mysqli_query($con,'UPDATE user SET token="'.mysqli_real_escape_string($con, $token->refresh_token).'" WHERE user_id='.$_SESSION['user_id']);
}
}
if ($client->getAccessToken()) {
$analytics = new Google_Service_Analytics($client);
$accounts = $analytics->management_accounts->listManagementAccounts();
$properties = $analytics->management_webproperties->listManagementWebproperties('~all');
$items = $properties->getItems();
$list = array();
$i = 0;
foreach ($items as $item) {
$list[$i]['name'] = $item->getName();
$list[$i]['nameurl'] = urlencode($name);
$list[$i]['id'] = $item->getAccountId();
$list[$i]['wpid'] = $item->getId();
$list[$i]['linkurl'] = urlencode($item->getChildLink()->getHref());
$i++;
}
} else {
$authUrl = $client->createAuthUrl();
header('Location: '.$authUrl);
}
I already have started with this one
ConnectionsController.php
<?php
use App\helpers\Common;
class ConnectionsController extends BaseController{
public function getExistingLogins($id){
$ga_user = DB::table('user')->select('*')->where('user_id', $id)->get();
$client_id = $ga_user[0]->client_id;
$client_secret = $ga_user[0]->client_secret;
$token = $ga_user[0]->token;
$api_key = 'API_KEY_HERE';
$application_name = 'APPLICATION_NAME';
$return = new App\Services\GoogleAnalytics($client_id, $client_secret, $application_name, $api_key, $token);
return $return->get();
}
return false;
}
App\Services\GoogleAnalytics.php
<?php
namespace App\Services;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Session;
class GoogleAnalytics {
protected $client;
protected $service;
protected $url;
function __construct($client_id, $client_secret, $application_name, $api_key, $token) {
$host = $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$http = (isset($_SERVER['HTTP_SCHEMETYPE']) && $_SERVER['HTTP_SCHEMETYPE'] == 'https') ? 'https:' : 'http:';
$this->url = $http . '//' . $host;
/* Set config variables */
$this->client = new \Google_Client();
$this->client->setApplicationName($application_name);
$this->client->setClientId($client_id);
$this->client->setClientSecret($client_secret);
$this->client->setRedirectUri($this->url);
$this->client->addScope("https://www.googleapis.com/auth/analytics.readonly");
$this->client->setApprovalPrompt('force');
$this->client->setAccessType('offline');
$this->client->refreshToken($token);
$this->service = new \Google_Service_Analytics($this->client);
}
public function get(){
$accounts = $this->service->management_accounts->listManagementAccounts();
$properties = $this->service->management_webproperties->listManagementWebproperties('~all');
$items = $properties->getItems();
$list = array();
$i = 0;
foreach ($items as $item) {
$list[$i]['name'] = $item->getName();
$list[$i]['nameurl'] = urlencode($name);
$list[$i]['id'] = $item->getAccountId();
$list[$i]['wpid'] = $item->getId();
$list[$i]['linkurl'] = urlencode($item->getChildLink()->getHref());
$i++;
}
return $list;
}
But I am having trouble with redirect. I am now stuck. How should I proceed? Any help is very much appreciated. Thanks.
You can use
Google API PHP Client (Laravel Bundle)
Which is a much simpler way to integrate google-api.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With