|
16 | 16 | */ |
17 | 17 |
|
18 | 18 | # [START documentai_quickstart] |
19 | | -# Includes the autoloader for libraries installed with composer |
| 19 | +# Include the autoloader for libraries installed with Composer. |
20 | 20 | require __DIR__ . '/vendor/autoload.php'; |
21 | 21 |
|
22 | | -# Imports the Google Cloud client library |
23 | | -use Google\Cloud\DocumentAI\V1\DocumentProcessorServiceClient; |
| 22 | +# Import the Google Cloud client library. |
| 23 | +use Google\Cloud\DocumentAI\V1\Client\DocumentProcessorServiceClient; |
24 | 24 | use Google\Cloud\DocumentAI\V1\RawDocument; |
| 25 | +use Google\Cloud\DocumentAI\V1\ProcessRequest; |
25 | 26 |
|
26 | | -$projectId = 'YOUR_PROJECT_ID'; # Your Google Cloud Platform project ID |
27 | | -$location = 'us'; # Your Processor Location |
28 | | -$processor = 'YOUR_PROCESSOR_ID'; # Your Processor ID |
| 27 | +# TODO(developer): Update the following lines before running the sample. |
| 28 | +# Your Google Cloud Platform project ID. |
| 29 | +$projectId = 'YOUR_PROJECT_ID'; |
29 | 30 |
|
30 | | -# Create Client |
31 | | -$client = new DocumentProcessorServiceClient(); |
| 31 | +# Your Processor Location. |
| 32 | +$location = 'us'; |
| 33 | + |
| 34 | +# Your Processor ID as hexadecimal characters. |
| 35 | +# Not to be confused with the Processor Display Name. |
| 36 | +$processorId = 'YOUR_PROCESSOR_ID'; |
32 | 37 |
|
33 | | -# Local File Path |
| 38 | +# Path for the file to read. |
34 | 39 | $documentPath = 'resources/invoice.pdf'; |
35 | 40 |
|
36 | | -# Read in File Contents |
| 41 | +# Create Client. |
| 42 | +$client = new DocumentProcessorServiceClient(); |
| 43 | + |
| 44 | +# Read in file. |
37 | 45 | $handle = fopen($documentPath, 'rb'); |
38 | 46 | $contents = fread($handle, filesize($documentPath)); |
39 | 47 | fclose($handle); |
40 | 48 |
|
41 | | -# Load File Contents into RawDocument |
42 | | -$rawDocument = new RawDocument([ |
43 | | - 'content' => $contents, |
44 | | - 'mime_type' => 'application/pdf' |
45 | | -]); |
| 49 | +# Load file contents into a RawDocument. |
| 50 | +$rawDocument = (new RawDocument()) |
| 51 | + ->setContent($contents) |
| 52 | + ->SetMimeType('application/pdf'); |
46 | 53 |
|
47 | | -# Fully-qualified Processor Name |
48 | | -$name = $client->processorName($projectId, $location, $processor); |
| 54 | +# Get the Fully-qualified Processor Name. |
| 55 | +$fullProcessorName = $client->processorName($projectId, $location, $processorId); |
49 | 56 |
|
50 | | -# Make Processing Request |
51 | | -$response = $client->processDocument($name, [ |
52 | | - 'rawDocument' => $rawDocument |
53 | | -]); |
| 57 | +# Send a ProcessRequest and get a ProcessResponse. |
| 58 | +$request = (new ProcessRequest()) |
| 59 | + ->setName($fullProcessorName) |
| 60 | + ->setRawDocument($rawDocument); |
54 | 61 |
|
55 | | -# Print Document Text |
56 | | -printf('Document Text: %s', $response->getDocument()->getText()); |
| 62 | +$response = $client->processDocument($request); |
57 | 63 |
|
| 64 | +# Show the text found in the document. |
| 65 | +printf('Document Text: %s', $response->getDocument()->getText()); |
58 | 66 | # [END documentai_quickstart] |
0 commit comments