【问题标题】:Google Vision API - Invalid JSON payload received. Unable to parse numberGoogle Vision API - 收到无效的 JSON 有效负载。无法解析号码
【发布时间】:2016-06-19 18:48:00
【问题描述】:

我正在使用 google Vision API。

当我在命令行中 curl 时,它会使用以下命令为我提供状态 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 一起使用时,我会收到一条返回消息:

{ "error": { "code": 400, "message": "收到无效的 JSON 有效负载。无法解析数字。\n------------------ --\n^", "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

【问题讨论】:

  • 如果我记得传递 json 需要在 php 中使用特殊的 json 标头...查找链接...

标签: php json curl google-vision


【解决方案1】:

请设置以下选项:

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 数据而不是 json 文件来解决它。谢谢弗拉基米尔的建议。
【解决方案2】:

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

【讨论】:

  • 能解决你的问题吗?我也确实在 curl 请求时遇到了白色问题。
猜你喜欢
  • 2020-12-01
  • 2018-04-04
  • 2018-07-31
  • 1970-01-01
  • 2016-12-27
  • 1970-01-01
  • 1970-01-01
  • 2019-12-13
  • 2020-06-26
相关资源
最近更新 更多