我正在使用谷歌视觉 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 一起使用时,我收到一条返回消息:
{ "error": { "code": 400, "message": "收到无效的 JSON 有效负载。无法解析数字。\n--------------------\ n^", "状态": "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);
}
我在关注这个例子: