如何有效处理Google Vision API Client Lib响应?

I'm new to Google Vision API Client Lib

I'm using Vision API Client Lib for PHP to detect text in images, this is my code:

<?php
require 'vendor/autoload.php';

use Google\Cloud\Vision\VisionClient;
function object_to_array($object) {
    return (array) $object;
}
$vision = new VisionClient(
['keyFile' => json_decode(file_get_contents("smartcity-credentials.json"), true)]
);
$img = file_get_contents('img2.jpg');
$image=$vision->image($img,['DOCUMENT_TEXT_DETECTION']);
$result=$vision->annotate($image);
$res=object_to_array($result);
var_dump($res);
?>

All I need is using response as an array for handling, but $result returns something likes array of object/object ( sorry because I don't know much of OOP/Object)

Although i convert $result to array $res, but if I use foreach loop

foreach ($res as $key=>$value){
    echo $value;
    echo '<br>';
}

I get this

Catchable fatal error: Object of class Google\Cloud\Vision\Annotation\Document could not be converted to string

How do we get value (text detected) in above response for using ?.

You should use the methods fullText() and text() in order to access to the detected text, something like this:

$document = $annotation->fullText();
$text = $document->text();

See the specification of these classes Annotation and Document.

you cannot use $value of type Document as string with the echo. use print_r($annotation); to see what you even get returned. this document text detection example looks quite alike, notice the nested foreach loops there. also see the documentation:

use Google\Cloud\Vision\VisionClient;

$vision = new VisionClient();
$imageResource = fopen(__DIR__.'/assets/the-constitution.jpg', 'r');
$image = $vision->image($imageResource, ['DOCUMENT_TEXT_DETECTION']);

$annotation = $vision->annotate($image);
$document = $annotation->fullText();

$info = $document->info();
$pages = $document->pages();
$text = $document->text();

here's some more examples; in particular the detect_document_text.php.