2016-05-26 7 views
6

次のコードを実際にlaravelに移植できるかどうかは疑問でしたか?laravel 5のgoogle analyticsにGoogle PHP APIを追加する方法

<?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); 
} 

私はすでに、この1

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; 
} 

を開始している。しかし、私は、リダイレクトとのトラブルを抱えています。私は今、立ち往生しています。私はどのように進めるべきですか?どんな助力も非常に感謝しています。ありがとう。

答えて

1

..魔法のように

+1

仕事.. !! –

関連する問題