abdulbaquee / unofficial-google-my-business
Google My Business API v4 的 PHP 库
dev-master
2024-06-23 17:42 UTC
Requires
- php: >=5.4
- ext-curl: *
- ext-json: *
- ext-mbstring: *
- ext-openssl: *
This package is auto-updated.
Last update: 2024-09-23 18:11:12 UTC
README
Google My Business API v4 的 PHP 库
Google My Business API V4 的简化版本
Google My Business
版本:4.0
网站: webgrapple.com
作者: abdulbaquee
使用方法
此应用程序需要 Google My Business API v4.0
配置
首先,在 config.php
中设置您的配置
<?php
session_start();
require 'vendor/autoload.php';
define('GMB_CLIENT_ID', '');
define('GMB_CLIENT_SECRET', '');
define('GMB_REDIRECT_URI', '');
$scopes = array(
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/business.manage'
);
define('SCOPE', $scopes);
登录
创建一个 login.php
文件以启动 OAuth2 登录过程
<?php
require './config.php';
$param = array(
'client_id' => GMB_CLIENT_ID,
'client_secret' => GMB_CLIENT_SECRET,
'redirect_uri' => GMB_REDIRECT_URI,
'scope' => SCOPE
);
$myBusiness = new GoogleBusinessProfile($param);
echo "<a href='" . $myBusiness->gmb_login() . "'>Login with Google</a>";
成功回调
创建一个 success.php
文件以处理 OAuth2 回调并检索访问令牌
<?php
require './config.php';
$param = array(
'client_id' => GMB_CLIENT_ID,
'client_secret' => GMB_CLIENT_SECRET,
'redirect_uri' => GMB_REDIRECT_URI,
'scope' => SCOPE
);
$myBusiness = new GoogleBusinessProfile($param);
$code = filter_input(INPUT_GET, 'code');
if (!isset($code) || empty($code)) {
header('Location: login.php');
exit;
}
$access_token = $myBusiness->get_access_token($code);
if (isset($access_token['error'])) {
echo "<p style='color: red; font-weight: bold;'>Errors: " . $access_token['error'] . " => " . $access_token['error_description'] . "</p>";
echo "<p><a href='login.php'>Back to Login page</a></p>";
exit;
}
$_SESSION['refresh_token'] = $access_token['refresh_token'];
header('Location: accounts.php');
exit;
列出位置
创建一个 location.php
文件以检索并显示账户的位置列表
<?php
require './config.php';
$param = array(
'client_id' => GMB_CLIENT_ID,
'client_secret' => GMB_CLIENT_SECRET,
'redirect_uri' => GMB_REDIRECT_URI,
'scope' => SCOPE
);
$myBusiness = new GoogleBusinessProfile($param);
$refresh_token = isset($_SESSION['refresh_token']) ? trim($_SESSION['refresh_token']) : NULL;
if (!isset($refresh_token) || empty($refresh_token)) {
header('Location: login.php');
exit;
}
$access_token = $myBusiness->get_exchange_token($refresh_token);
if (!isset($access_token['access_token'])) {
header('Location: login.php');
exit;
}
if (!isset($_SESSION['gmb_account_name'])) {
header('Location: login.php');
exit;
}
$mask = array('title', 'name', 'phoneNumbers', 'storefrontAddress', 'websiteUri', 'metadata');
$readMask['readMask'] = implode(',', $mask);
$locations = $myBusiness->get_locations($_SESSION['gmb_account_name'], $access_token['access_token'], $readMask);
echo "<pre>";
print_r($locations);
位置详情
创建一个 locations_details.php
文件以检索并显示特定位置的详细信息
<?php
require './config.php';
$param = array(
'client_id' => GMB_CLIENT_ID,
'client_secret' => GMB_CLIENT_SECRET,
'redirect_uri' => GMB_REDIRECT_URI,
'scope' => SCOPE
);
define('LOCATION_NAME', 'locations/12301955069276590370');
$myBusiness = new GoogleBusinessProfile($param);
$refresh_token = isset($_SESSION['refresh_token']) ? trim($_SESSION['refresh_token']) : NULL;
if (!isset($refresh_token) || empty($refresh_token)) {
header('Location: login.php');
exit;
}
$access_token = $myBusiness->get_exchange_token($refresh_token);
if (!isset($access_token['access_token'])) {
header('Location: login.php');
exit;
}
if (!isset($_SESSION['gmb_account_name'])) {
header('Location: login.php');
exit;
}
$mask = array('title', 'name', 'phoneNumbers', 'storefrontAddress', 'websiteUri', 'metadata');
$readMask['readMask'] = implode(',', $mask);
$location_details = $myBusiness->get_locations_details(LOCATION_NAME, $access_token['access_token'], $readMask);
echo "<pre>";
print_r($location_details);
echo "</pre>";
更新
关注此库的更新,以确保与 Google My Business API 的最新版本兼容。