Google Vision API - 收到的 JSON 有效负载无效.无法解析数字


Google Vision API - Invalid JSON payload received. Unable to parse number

我正在使用Google Vision API。

当我在命令行中卷曲时,它通过以下命令给我状态 200 OK:

curl -v -k -s -H "Content-Type: application/json" https://vision.googleapis.com/v1/images:annotate?key=API_KEY --data-binary @base64.json

但是当我在PHP中使用它时,我会收到一条返回消息:

{ "错误": { "代码": 400, "消息": "收到的 JSON 有效负载无效。无法解析数字。'--------------------'^", "status": "INVALID_ARGUMENT" } }

try {
$post = array(
    'file' => '@base64.json'
);
$ch = curl_init('https://vision.googleapis.com/v1/images:annotate?key=API_KEY');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $content = curl_exec($ch);
    if (FALSE === $content)
        throw new Exception(curl_error($ch), curl_errno($ch));
    curl_close($ch);  // Seems like good practice
    return $content;
} catch (Exception $e) {
    trigger_error(sprintf(
        'Curl failed with error #%d: %s',
        $e->getCode(), $e->getMessage()),
        E_USER_ERROR);
}

我正在遵循此示例:

https://cloud.google.com/vision/docs/getting-started

请设置以下选项:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
);

http://alvinalexander.com/php/php-curl-examples-curl_setopt-json-rest-web-service

我遇到了同样的问题。我的问题是 json 内容中存在的换行符。一旦我删除了所有的空格,我又开始得到 200。