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

使用google-cloud-php调用Google Document AI出现INVALID_ARGUMENT错误如何解决

问题核心原因

你遇到的参数错误和mime_type命名无关,谷歌PHP客户端的protobuf类统一使用下划线风格的参数键,你看到的REST API文档的驼峰mimeType是HTTP请求体的格式,PHP客户端会自动做转换,不需要你手动修改,改了反而会触发类参数异常。
报错的核心大概率是跨区域端点不匹配、处理器路径参数错误或者请求传参格式不符合最新客户端要求。


可直接运行的修正代码

require 'vendor/autoload.php';

putenv('GOOGLE_APPLICATION_CREDENTIALS=[你的服务账号密钥路径].json');

use Google\Cloud\DocumentAI\V1\Document;
use Google\Cloud\DocumentAI\V1\DocumentProcessorServiceClient;

// 重点1:使用欧洲区域的端点,因为你的处理器在eu区域,默认是美国端点,跨区域调用直接报参数错误
$options = [
    'apiEndpoint' => 'eu-documentai.googleapis.com'
];
$documentProcessorServiceClient = new DocumentProcessorServiceClient($options);

try {
    // 重点2:processorName参数顺序严格是 项目ID、区域、处理器ID,不要填错顺序
    $processorName = $documentProcessorServiceClient->processorName(
        '[你的GCP项目ID]',
        'eu',
        '[你的处理器ID]'
    );

    // Document参数保持下划线即可,客户端会自动转换为驼峰传给API
    $pdfContent = file_get_contents('file.pdf');
    if ($pdfContent === false) {
        throw new \Exception('PDF文件读取失败,请检查路径是否正确');
    }
    $document = (new Document())
        ->setMimeType('application/pdf')
        ->setContent($pdfContent);

    // 重点3:processDocument的传参格式调整,明确指定参数名
    $response = $documentProcessorServiceClient->processDocument($processorName, [
        'inlineDocument' => $document,
        'skipHumanReview' => true
    ]);

    // 处理返回结果
    print_r($response->getDocument());
} finally {
    $documentProcessorServiceClient->close();
}

额外排查项

  • 确认你的服务账号有roles/documentai.editor或者对应处理器的调用权限
  • 确认PDF文件大小不超过Document AI的单文件限制:同步处理最大20页、100MB,超出的话需要用异步处理接口
  • 确认你填写的处理器状态是已启用,没有被禁用或删除

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 19:45:03