简体   繁体   中英

Google Cloud Vision - PHP Error occurred during parsing

Im using Vision API Client Library for PHP. This is my code:

use Google\Cloud\Vision\V1\ImageAnnotatorClient;
putenv("GOOGLE_APPLICATION_CREDENTIALS=/json.json");
$imageAnnotator = new ImageAnnotatorClient();
$fileName = 'textinjpeg.jpg';
$image = file_get_contents($fileName);
$response = $imageAnnotator->labelDetection($image);
$labels = $response->getLabelAnnotations();
if ($labels) {
    echo("Labels:" . PHP_EOL);
    foreach ($labels as $label) {
        echo($label->getDescription() . PHP_EOL);
    }
} else {
    echo('No label found' . PHP_EOL);
}

And I receive this error:

Error occurred during parsing: Fail to push limit. (0)
/srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/CodedInputStream.php:345
#0: Google\Protobuf\Internal\CodedInputStream->pushLimit(integer)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/CodedInputStream.php:368
#1: Google\Protobuf\Internal\CodedInputStream->incrementRecursionDepthAndPushLimit(integer, integer, integer)
....
....
....
#15: Google\Cloud\Vision\V1\ImageAnnotatorClient->labelDetection(string)
/srv/www/site.ru/htdocs/local/php_interface/GoogleCloud.php:41

This is the place, where Exception goes from:

public function pushLimit($byte_limit)
{
    // Current position relative to the beginning of the stream.
    $current_position = $this->current();
    $old_limit = $this->current_limit;

    // security: byte_limit is possibly evil, so check for negative values
    // and overflow.
    if ($byte_limit >= 0 &&
        $byte_limit <= PHP_INT_MAX - $current_position &&
        $byte_limit <= $this->current_limit - $current_position) {
        $this->current_limit = $current_position + $byte_limit;
        $this->recomputeBufferLimits();
    } else {
        throw new GPBDecodeException("Fail to push limit.");
    }

    return $old_limit;
}

$byte_limit <= $this->current_limit - $current_position is true

Should I increase current_position? And if I should, how can i do it? Change something on server or in PHP config?

You mentioned that $byte_limit <= $this->current_limit - $current_position is true, so either $byte_limit >= 0 or $byte_limit <= PHP_INT_MAX - $current_position are false.

If $byte_limit <= PHP_INT_MAX - $current_position is false, then increasing $current_position , won't turn it true. If you want to tweak the values, so the expression get evaluated as true, you would need to increase the value of PHP_INT_MAX instead .

If $byte_limit >= 0 is false, then modifying $current_limit won't avoid the exception.

Either way, it seems that the error is an issue with the protobuf php library, so I'd recommend you to report the issue there , rather than try to modify the values directly.

mbstring.func_overload 是 2 这是错误的原因更改为 0 并且它起作用了

It's a crazy thing to do!

The error "Fail to push limit" appears from time to time in forums. Various ideas are given there as to where the problem could lie. One cause could be when the source code is composed on the local PC via Composer and then transferred to the server via (S)FTP . The FTP programme decides on the basis of the file extension whether it saves the data on the server in ASCII or binary format.

In vendor/google/protobuf/src/Google/Protobuf/ there are various generated files that have a .php extension but are actually BINARY! (if you open the file, you can see it immediately, eg : vendor/google/protobuf/src/GPBMetadata/Google/Protobuf/Any.php) The solution to transfer these files explicitly via binary to the server worked in my case! If in doubt, transfer the complete module from Google/protobuf as a binary...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM