when I run the sample code from the offical Quickstart guide I get
Uncaught Error: Class "Google\Cloud\DocumentAI\V1\DocumentProcessorServiceClient" not found
Running PHP version 8.4.3, I'm using this guide https://cloud.google.com/document-ai/docs/samples/documentai-quickstart?hl=en
index.php
<?php
//auth
$authpath="Authfile.json";
putenv("GOOGLE_APPLICATION_CREDENTIALS=$authpath");
//load librarys
require_once 'vendor/autoload.php'
# Imports the Google Cloud client library
use Google\Cloud\DocumentAI\V1\DocumentProcessorServiceClient;
use Google\Cloud\DocumentAI\V1\RawDocument;
$projectId = 'myprojectid'; # Your Google Cloud Platform project ID
$location = 'us'; # Your Processor Location
$processor = 'c......1'; # Your Processor ID
# Create Client
$client = new DocumentProcessorServiceClient();
# Local File Path
$documentPath = 'Winnie_the_Pooh_3_Pages-1.pdf';
# Read in File Contents
$handle = fopen($documentPath, 'rb');
$contents = fread($handle, filesize($documentPath));
fclose($handle);
# Load File Contents into RawDocument
$rawDocument = new RawDocument([
'content' => $contents,
'mime_type' => 'application/pdf'
]);
# Fully-qualified Processor Name
$name = $client->processorName($projectId, $location, $processor);
# Make Processing Request
$response = $client->processDocument($name, [
'rawDocument' => $rawDocument
]);
# Print Document Text
printf('Document Text: %s', $response->getDocument()->getText());
?>
I figured it out, the code sample was out of date
use Google\Cloud\DocumentAI\V1\DocumentProcessorServiceClient;
is bad and no longer works.
use Google\Cloud\DocumentAI\V1\Client\DocumentProcessorServiceClient;
is what you need. apparently you need to add "\Client" after the v1 now for it to find it and according to this guide it applies to all clients but not all use items. just the clients also the way the client reads the file is also quite different now, this is what worked:
<?php
//auth
$authpath="your.json";
putenv("GOOGLE_APPLICATION_CREDENTIALS=$authpath");
//load library's
require_once 'vendor/autoload.php';
# Imports the Google Cloud client library
use Google\Cloud\DocumentAI\V1\Client\DocumentProcessorServiceClient;
use Google\Cloud\DocumentAI\V1\ProcessRequest;
use Google\Cloud\DocumentAI\V1\ProcessResponse;
use Google\Cloud\DocumentAI\V1\RawDocument;
$projectId = 'your project name'; # Your Google Cloud Platform project ID
$location = 'us'; # Your Processor Location
$processor = 'c2c7b6e91fc07b01'; # Your Processor ID
# Local File Path
$documentPath = 'Winnie_the_Pooh_3_Pages.pdf';
// Create a client.
$documentProcessorServiceClient = new DocumentProcessorServiceClient();
// Prepare the request message.
//processor
$pro="projects/".$projectId."/locations/".$location."/processors/".$processor;
//document
# Load File Contents into RawDocument
$rawDocument = new RawDocument(['content' => file_get_contents($documentPath),'mime_type' => 'application/pdf']);
$request = (new ProcessRequest())->setName($pro)->setRawDocument($rawDocument);
try {
$response = $documentProcessorServiceClient->processDocument($request);
printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
}
catch (ApiException $ex)
{
printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
}
?>
I also found out that if you get the error that it can't find the ADC a work around is to add it in the client creation
$documentProcessorServiceClient = new DocumentProcessorServiceClient(['credentials' => "path to your json"]);