PHP集成Google Index API遇500空白页错误求助
Let's work through your 500 error issue step by step—since you can't use Composer on your Chromebook, we'll focus on fixing the manual setup problems.
1. First: Reveal the Actual Error (No More Blank Page)
A 500 error with a blank page usually means PHP is suppressing errors. Add these lines at the very top of your index.php to see what's going wrong:
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
This will show you the specific error message (like a missing file, invalid path, or authentication issue) instead of just a blank screen.
2. Fix the Autoloader Path & Reference
Your current autoload setup is using a deprecated compatibility file, and the path is likely incorrect. Here's how to fix it:
- If you downloaded the full Google API Client Library (including the
vendordirectory), update your require line inindex.phpto point to the correct autoloader:require_once 'google-api-php-client/vendor/autoload.php'; - Remove the old
src/Google/autoload.phpreference entirely—this file is only for backwards compatibility and triggers deprecated warnings that can break your script.
3. Correct the Service Account File Path
Your index.php is already in the /indexapi/ directory, so your current path indexapi/service_account_file.json is looking for a file at /indexapi/indexapi/service_account_file.json (which doesn't exist). Simplify it to:
$client->setAuthConfig('service_account_file.json');
4. Fix the API Request Format
Your raw JSON string might cause formatting issues, and you're missing the required Content-Type header. Update your request code to:
// Use json_encode to ensure valid JSON $content = json_encode([ "url" => "https://myurl2020.com", "type" => "URL_UPDATED" ]); // Add the Content-Type header so Google recognizes the JSON body $response = $httpClient->post($endpoint, [ 'headers' => ['Content-Type' => 'application/json'], 'body' => $content ]);
5. Double-Check Prerequisites
Don't forget to confirm these critical steps from the official docs:
- The service account from your JSON key has been added as an owner of your site in Google Search Console.
- The URL you're submitting (
https://myurl2020.com) is verified in Search Console under the same property.
Full Modified Code Example
Here's what your index.php should look like after all fixes:
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); require_once 'google-api-php-client/vendor/autoload.php'; $client = new Google_Client(); $client->setAuthConfig('service_account_file.json'); $client->addScope('https://www.googleapis.com/auth/indexing'); $httpClient = $client->authorize(); $endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish'; $content = json_encode([ "url" => "https://myurl2020.com", "type" => "URL_UPDATED" ]); $response = $httpClient->post($endpoint, [ 'headers' => ['Content-Type' => 'application/json'], 'body' => $content ]); $status_code = $response->getStatusCode(); print_r($response); echo "\n"; echo "$status_code";
If you still get errors after these changes, share the specific error message from the screen—this will help narrow down remaining issues like authentication failures or permission problems.
内容的提问来源于stack exchange,提问作者Seb

