setClientId($clien-6ren">
gpt4 book ai didi

php - 如何在 laravel 5 中为谷歌分析添加谷歌 PHP API

转载 作者:可可西里 更新时间:2023-11-01 13:27:34 27 4
gpt4 key购买 nike

所以,我想知道下面的代码是否真的可以移植到 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);
}

这个我已经开始了

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

应用\服务\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;
}

但是我在重定向方面遇到了问题。我现在卡住了。我该如何进行?很感谢任何形式的帮助。谢谢。

最佳答案

你可以使用

Google API PHP Client (Laravel Bundle)

这是一种更简单的集成 google-api 的方法。

关于php - 如何在 laravel 5 中为谷歌分析添加谷歌 PHP API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37470787/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com