You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用AWS SDK listObjects时Guzzle函数未定义错误的非源码修改方案问询

解决AWS SDK调用listObjects时出现GuzzleHttp函数未定义错误

错误详情

调用AWS SDK的listObjects方法获取对象时,触发PHP致命错误:

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

业务代码

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.");
}

发现将错误行的调用改为\GuzzleHttp\Utils::defaultUserAgent()可消除错误,但不想修改库源码。

堆栈跟踪

[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

环境信息

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

已尝试操作

  • 将PHP版本改为8+
  • 降级至多个其他aws/aws-sdk-php版本
  • 使用其他凭证方式

解决方案(无需修改库源码)

1. 修复Guzzle版本兼容性

这个错误根源是AWS SDK与Guzzle版本不匹配:default_user_agent()是Guzzle 6的全局函数,若项目中安装的是Guzzle 7,该函数已迁移到GuzzleHttp\Utils::defaultUserAgent()静态方法。

  • 执行composer show guzzlehttp/guzzle查看当前Guzzle版本
  • 在composer.json中添加明确的版本约束,确保Guzzle版本与AWS SDK兼容:
    "require": {
        "aws/aws-sdk-php": "3.281.6",
        "guzzlehttp/guzzle": "^6.5"
    }
    
  • 执行composer update重新安装依赖

2. 手动指定HTTP Handler

绕过SDK自带的GuzzleV6 Handler,直接传入兼容的Guzzle客户端实例:

require_once '/path/to/autoload.php';

// 创建Guzzle客户端
$guzzleClient = new \GuzzleHttp\Client();

$client = new \Aws\S3\S3Client([
    'region' => 'us-west-1',
    'credentials' => [
        'key'    =>'XXXXXXXXXXXXXXXXXX',
        'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXX',
    ],
    'http_handler' => $guzzleClient
]);

3. 清理Composer缓存并重装依赖

缓存可能导致依赖版本混乱,执行以下命令重置依赖:

composer clear-cache
composer install --no-dev

内容的提问来源于stack exchange,提问作者Farid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 22:15:54