私は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 で使用すると、次のメッセージが返されます。
{ "エラー": { "コード": 400、"メッセージ": "無効な 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);
}
私はこの例に従っていました: