Skip to content

Commit dbf6d80

Browse files
fix(documentai): fix 'quickstart' for latest client-library (#2076)
1 parent ddc3942 commit dbf6d80

File tree

3 files changed

+54
-45
lines changed

3 files changed

+54
-45
lines changed

documentai/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"require": {
3-
"google/cloud-document-ai": "^1.0.1"
3+
"google/cloud-document-ai": "^2.1.3"
44
}
55
}

documentai/phpunit.xml.dist

+22-21
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,26 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
-->
17-
<phpunit bootstrap="../testing/bootstrap.php">
18-
<testsuites>
19-
<testsuite name="PHP documentai test">
20-
<directory>test</directory>
21-
</testsuite>
22-
</testsuites>
23-
<logging>
24-
<log type="coverage-clover" target="build/logs/clover.xml"/>
25-
</logging>
26-
<filter>
27-
<whitelist>
28-
<directory suffix=".php">./src</directory>
29-
<file>quickstart.php</file>
30-
<exclude>
31-
<directory>./vendor</directory>
32-
</exclude>
33-
</whitelist>
34-
</filter>
35-
<php>
36-
<env name="PHPUNIT_TESTS" value="1"/>
37-
</php>
17+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="../testing/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
18+
<coverage>
19+
<include>
20+
<directory suffix=".php">./src</directory>
21+
<file>quickstart.php</file>
22+
</include>
23+
<exclude>
24+
<directory>./vendor</directory>
25+
</exclude>
26+
<report>
27+
<clover outputFile="build/logs/clover.xml"/>
28+
</report>
29+
</coverage>
30+
<testsuites>
31+
<testsuite name="PHP documentai test">
32+
<directory>test</directory>
33+
</testsuite>
34+
</testsuites>
35+
<logging/>
36+
<php>
37+
<env name="PHPUNIT_TESTS" value="1"/>
38+
</php>
3839
</phpunit>

documentai/quickstart.php

+31-23
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,51 @@
1616
*/
1717

1818
# [START documentai_quickstart]
19-
# Includes the autoloader for libraries installed with composer
19+
# Include the autoloader for libraries installed with Composer.
2020
require __DIR__ . '/vendor/autoload.php';
2121

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;
2424
use Google\Cloud\DocumentAI\V1\RawDocument;
25+
use Google\Cloud\DocumentAI\V1\ProcessRequest;
2526

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';
2930

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';
3237

33-
# Local File Path
38+
# Path for the file to read.
3439
$documentPath = 'resources/invoice.pdf';
3540

36-
# Read in File Contents
41+
# Create Client.
42+
$client = new DocumentProcessorServiceClient();
43+
44+
# Read in file.
3745
$handle = fopen($documentPath, 'rb');
3846
$contents = fread($handle, filesize($documentPath));
3947
fclose($handle);
4048

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');
4653

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);
4956

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);
5461

55-
# Print Document Text
56-
printf('Document Text: %s', $response->getDocument()->getText());
62+
$response = $client->processDocument($request);
5763

64+
# Show the text found in the document.
65+
printf('Document Text: %s', $response->getDocument()->getText());
5866
# [END documentai_quickstart]

0 commit comments

Comments
 (0)