服务器新增页面引入config.php遇PHP错误及Google服务异常求助
问题分析与解决方案
出现的错误
- PHP致命错误:未捕获的
Google_Service_Exception,返回404错误,提示Requested entity was not found. - PHP警告:
Undefined index: REQUEST_URI(位于/home/eblatech/insights.ebla-tech.com/admin/includes/config.php第7行) - PHP警告:
Undefined index: HTTP_HOST(同上文件第7行)
相关代码文件
config.php 内容
<?php session_set_cookie_params(2*3600,"/"); session_start(); $APP_ROOT = "../admin/"; $FILES_ROOT = "/insights-dashboard/admin/files/"; $MODEL_ROOT = realpath(dirname(__FILE__) . '/../../'); $actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; define("SESSION", "insightEbla"); define("DEFAULT_LANGUAGE", "en"); // var_dump($_SESSION); // exit; if (!isset($_SESSION[SESSION]['lang']) || empty($_SESSION[SESSION]['lang'])) { $_SESSION[SESSION]['lang'] = DEFAULT_LANGUAGE; }; $current_lang = $_SESSION[SESSION]['lang']; if ($_SESSION[SESSION]['lang'] == 'ar') { require_once "lang/ar.php"; } else { require_once "lang/en.php"; } require_once "page_names.php"; require_once "enum.php"; require_once $MODEL_ROOT . '/model/db_endpoint.php'; require_once "authentication.php"; require_once "functions.php"; require_once "authorization.php"; require_once "pages_includes.php";
主调试代码
<?php include_once '../../google-api-php-client-2.4.0/vendor/autoload.php'; // or wherever autoload.php is located include_once './MyBusiness.php'; ini_set('max_execution_time', '0'); include_once "../../includes/config.php"; $client = new Google_Client(); if (isset($_SERVER['HTTP_HOST']) && ($_SERVER['HTTP_HOST'] == "localhost")) { $client->setAuthConfig('../client_secret_for_Google_my_bussniss.json'); $json = json_decode(file_get_contents('../Google-My-Business-tokens.json')); }else{ $client->setAuthConfig('/home/eblatech/insights-secrets/client_secret_for_Google_my_bussniss.json'); $json = json_decode(file_get_contents('/home/eblatech/insights-secrets/Google-My-Business-tokens.json')); } $client->addScope('https://www.googleapis.com/auth/business.manage'); $refreshToken = $json->refreshToken; $_SESSION['access_token']=$json->access_token; if (isset($refreshToken) && $refreshToken) { $client->refreshToken($refreshToken); $mybusinessService = new Google_Service_Mybusiness($client); $posts = $mybusinessService->accounts_locations_localPosts; $x=$posts->get("BlackSwanLimos"); echo '<pre>'; print_r($x); echo '</pre>';
错误修复步骤
1. 修复Undefined index警告
问题出在config.php第7行直接调用$_SERVER[HTTP_HOST]和$_SERVER[REQUEST_URI],这两个变量在非HTTP请求环境(如命令行执行)或特殊服务器配置下可能不存在,需先做存在性校验:
修改第7行代码为:
// 先获取安全的主机和URI值,不存在则用默认值兜底 $httpHost = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'insights.ebla-tech.com'; $requestUri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/'; // 修正HTTPS判断逻辑,更严谨 $actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$httpHost$requestUri";
如果脚本可能在命令行环境运行,可进一步优化:
if (php_sapi_name() === 'cli') { // 命令行环境直接指定域名 $actual_link = "https://insights.ebla-tech.com"; } else { $httpHost = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'insights.ebla-tech.com'; $requestUri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/'; $actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$httpHost$requestUri"; }
2. 修复Google API 404错误
Requested entity was not found错误说明调用$posts->get("BlackSwanLimos")时使用的资源ID格式错误。Google My Business API的accounts_locations_localPosts->get方法需要完整的资源名称,格式为accounts/{account_id}/locations/{location_id}/localPosts/{post_id},而非直接使用商家名称。
修复步骤:
- 先调用
accounts->listAccounts()获取账号ID - 再调用
accounts_locations->listAccountsLocations()获取对应账号下的门店ID - 最后调用
accounts_locations_localPosts->listAccountsLocationsLocalPosts()获取门店下的帖子ID,再用正确的资源ID调用get方法
示例代码:
// 获取账号列表 $accounts = $mybusinessService->accounts->listAccounts(); foreach ($accounts->getAccounts() as $account) { $accountId = $account->getName(); // 格式:accounts/123456 // 获取该账号下的门店 $locations = $mybusinessService->accounts_locations->listAccountsLocations($accountId); foreach ($locations->getLocations() as $location) { $locationId = $location->getName(); // 格式:accounts/123456/locations/789012 // 获取该门店下的帖子列表 $postsList = $mybusinessService->accounts_locations_localPosts->listAccountsLocationsLocalPosts($locationId); foreach ($postsList->getLocalPosts() as $post) { $postId = $post->getName(); // 格式:accounts/123456/locations/789012/localPosts/345678 // 获取具体帖子详情 $postDetail = $mybusinessService->accounts_locations_localPosts->get($postId); echo '<pre>'; print_r($postDetail); echo '</pre>'; } } }
同时需确认:你的Google账号有权限访问目标商家信息,且API密钥、令牌对应的项目已启用Google My Business API。
内容的提问来源于stack exchange,提问作者george derderian
相关产品推荐
相关产品推荐

