AWS Textract continues to be a topic of interest, even in 2023! I just got an email from someone who was having trouble getting my Textract demo from 2020 running.

So I take a look at my old code. I’m now running Ubuntu 22, and my php is v 8.1.2:

php --version
PHP 8.1.2-1ubuntu2.14 (cli) (built: Aug 18 2023 11:41:11) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2-1ubuntu2.14, Copyright (c), by Zend Technologies

I run my old code, and sure enough, it doesn’t work. The stack trace looks something like this:

php textract_demo_async.php
PHP Fatal error:  Uncaught Error: Call to undefined function JmesPath\mb_strlen() in TextractDemo/JmesPath/Lexer.php:343
Stack trace:
#0 TextractDemo/JmesPath/Parser.php(76): JmesPath\Lexer->tokenize()
#1 TextractDemo/JmesPath/AstRuntime.php(42): JmesPath\Parser->parse()
#2 TextractDemo/JmesPath/Env.php(33): JmesPath\AstRuntime->__invoke()
#3 TextractDemo/Aws/Endpoint/PartitionEndpointProvider.php(117): JmesPath\Env::search()
#4 TextractDemo/Aws/Endpoint/PartitionEndpointProvider.php(99): Aws\Endpoint\PartitionEndpointProvider::mergePrefixData()
#5 TextractDemo/Aws/ClientResolver.php(719): Aws\Endpoint\PartitionEndpointProvider::defaultProvider()
#6 TextractDemo/Aws/ClientResolver.php(388): Aws\ClientResolver::_apply_endpoint_provider()
#7 TextractDemo/Aws/AwsClient.php(222): Aws\ClientResolver->resolve()
#8 TextractDemo/textract_demo_async.php(30): Aws\AwsClient->__construct()
#9 {main}
  thrown in TextractDemo/JmesPath/Lexer.php on line 343

Okay, well, who expects code to run after so many years?

My first thought is, perhaps there’s a new AWS SDK or some update to comply with php v 8.1.

Following my own instructions from way back when, I mosey on over to the AWS installation instructions for their PHP SDK version 3. There, I download the SDK zip file. Sure enough, the files here are much more recent, from September 22 2023 (seems suspiciously recent tbh). I unzip those into a new directory, and copy most of my old demo files into that directory as well, being careful not to overwrite aws-autoloader.php.

But then I get the same error.

I take another look at the installation documentation, and notice that they say:

Note: Installing the SDK via the .phar and .zip methods requires the Multibyte String PHP extension to be installed and enabled separately.

Huh, what’s that? I search around and find this hint:

sudo apt install php8.0-mbstring

I’m on php8.1 so I try:

sudo apt install php8.1-mbstring

That did the trick. Now php textract_demo_async.php works just fine again. It surprised me that it was that easy to fix this problem! I hope this helps someone else.