Search code examples
phpwordpressaws-php-sdk

Uncaught Error: Call to undefined function GuzzleHttp\default_user_agent()


I am getting the following error while trying to retrieve objects using the listObjects method:

PHP Fatal error:  Uncaught Error: Call to undefined function GuzzleHttp\default_user_agent() in C:\...\wp-content\plugins\...\vendor\aws\aws-sdk-php\src\Handler\GuzzleV6\GuzzleHandler.php:40

My code:

require_once '/path/to/autoload.php';
    
        $client = new \Aws\S3\S3Client(array(
            'region' => 'us-west-1',
            'credentials' =>  [
                'key'    =>'XXXXXXXXXXXXXXXXXX',
                'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXX',
            ],
            
        ));
        
        $bucket_name = 'XXXXXXXX';
        
        try {
            $contents = $client->listObjects([
                'Bucket' => $bucket_name,
            ]);
            echo "The contents of your bucket are: \n";
            print_r($contents);
        } catch (S3Exception  $exception) {
            echo "Failed to list objects in $bucket_name with error: " . $exception->getMessage();
            exit("Please fix error with listing objects before continuing.");
        }

The error disappears if I change the function being called on line 40 to \GuzzleHttp\Utils::defaultUserAgent().

Stack trace:

[13-Sep-2023 19:33:13 UTC] PHP Fatal error:  Uncaught Error: Call to undefined function GuzzleHttp\default_user_agent() in C:\...\wp-content\plugins\...\vendor\aws\aws-sdk-php\src\Handler\GuzzleV6\GuzzleHandler.php:40
Stack trace:
#0 C:\...\wp-content\plugins\...\vendor\aws\aws-sdk-php\src\WrappedHttpHandler.php(88): Aws\Handler\GuzzleV6\GuzzleHandler->__invoke(Object(GuzzleHttp\Psr7\Request), Array)
#1 C:\...\wp-content\plugins\...\vendor\aws\aws-sdk-php\src\ClientSideMonitoring\AbstractMonitoringMiddleware.php(126): Aws\WrappedHttpHandler->__invoke(Object(Aws\Command), Object(GuzzleHttp\Psr7\Request))
#2 C:\...\wp-content\plugins\...\vendor\aws\aws-sdk-php\src\S3\PermanentRedirectMiddleware.php(43): Aws\ClientSideMonitoring\AbstractMonitoringMiddleware->__invoke(Object(Aws\Command), Object(GuzzleHttp\Psr7\Request))
#3 C:\...\wp-content\plugins\...\vendor\aws\aws-sdk-php\src\S3\PutObjectUrlMiddleware.php(41): Aws\S3\PermanentRedirectMiddleware->__invoke(Object(Aws\Command), Object(GuzzleHttp\Psr7\Request))
#4 C:\...\wp-content\plugins\...\vendor\aws\aws-sdk-php\src\Middleware.php(152): Aws\S3\PutObjectUrlMiddleware->__invoke(Object(Aws\Command), Object(GuzzleHttp\Psr7\Request))
#5 C:\...\wp-content\plugins\...\vendor\guzzlehttp\promises\src\FulfilledPromise.php(48): Aws\Middleware::Aws\{closure}(Object(Aws\Credentials\Credentials))
#6 C:\...\wp-content\plugins\...\vendor\guzzlehttp\promises\src\TaskQueue.php(52): GuzzleHttp\Promise\FulfilledPromise::GuzzleHttp\Promise\{closure}()
#7 C:\...\wp-content\plugins\...\vendor\guzzlehttp\promises\src\Promise.php(251): GuzzleHttp\Promise\TaskQueue->run(true)
#8 C:\...\wp-content\plugins\...\vendor\guzzlehttp\promises\src\Promise.php(227): GuzzleHttp\Promise\Promise->invokeWaitFn()
#9 C:\...\wp-content\plugins\...\vendor\guzzlehttp\promises\src\Promise.php(272): GuzzleHttp\Promise\Promise->waitIfPending()
#10 C:\...\wp-content\plugins\...\vendor\guzzlehttp\promises\src\Promise.php(229): GuzzleHttp\Promise\Promise->invokeWaitList()
#11 C:\...\wp-content\plugins\...\vendor\guzzlehttp\promises\src\Promise.php(69): GuzzleHttp\Promise\Promise->waitIfPending()
#12 C:\...\wp-content\plugins\...\vendor\aws\aws-sdk-php\src\AwsClientTrait.php(58): GuzzleHttp\Promise\Promise->wait()
#13 C:\...\wp-content\plugins\...\vendor\aws\aws-sdk-php\src\AwsClientTrait.php(86): Aws\AwsClient->execute(Object(Aws\Command))
#14 C:\...\wp-content\plugins\...\log-cleanup\...-log-cleanup.php(36): Aws\AwsClient->__call('listObjects', Array)
#15 C:\...\wp-content\plugins\...\log-cleanup\...-log-cleanup.php(98): wpp_integrations_log_cleanup_execute()
#16 C:\...\wp-includes\rest-api\class-wp-rest-server.php(1188): wpp_integrations_log_cleanup_handle_execute_request(Object(WP_REST_Request))
#17 C:\...\wp-includes\rest-api\class-wp-rest-server.php(1035): WP_REST_Server->respond_to_request(Object(WP_REST_Request), '/log_cleanup/v1...', Array, NULL)
#18 C:\...\wp-includes\rest-api\class-wp-rest-server.php(447): WP_REST_Server->dispatch(Object(WP_REST_Request))
#19 C:\...\wp-includes\rest-api.php(418): WP_REST_Server->serve_request('/log_cleanup/v1...')
#20 C:\...\wp-includes\class-wp-hook.php(310): rest_api_loaded(Object(WP))
#21 C:\...\wp-includes\class-wp-hook.php(334): WP_Hook->apply_filters('', Array)
#22 C:\...\wp-includes\plugin.php(565): WP_Hook->do_action(Array)
#23 C:\...\wp-includes\class-wp.php(398): do_action_ref_array('parse_request', Array)
#24 C:\...\wp-includes\class-wp.php(779): WP->parse_request('')
#25 C:\...\wp-includes\functions.php(1335): WP->main('')
#26 C:\...\wp-blog-header.php(16): wp()
#27 C:\...\index.php(17): require('C:\\Users\\...')
#28 {main}
  thrown in C:\...\wp-content\plugins\...\vendor\aws\aws-sdk-php\src\Handler\GuzzleV6\GuzzleHandler.php on line 40

Environment:

  • Windows 10
  • PHP 7.4.33
  • Composer 2.6.2
  • aws/aws-sdk-php 3.281.6

Things I tried:

  • Changing PHP version to 8+
  • Downgrading to several other aws/aws-sdk-php versions
  • Other credential methods

How can I avoid this error without having to edit the library's source code?


Solution

  • The issue was caused by a WordPress plugin (WP Analitify) that also has Guzzle in its source, and it had an older version.

    Updating Analitify to its latest version solved the issue.