PHP でのAPI呼び出し例

#!/usr/bin/php
<?php

$URL = 'https://xxxxx.dl.sony.com/v1/serverless/classifiers/dasdfadfd/inference';
$KEY = '6626b3e4-1316-4b1a-b10b-6dsaf3dfsdab995d8';
$PIC = './sample33-55.png';

$handle = fopen($PIC, "r");
$contents = fread($handle, filesize($PIC));
$b64 = base64_encode($contents);

$data = [
"executor" => "Executor",
"inputs" => [[
"name" => "x",
"type" => "png",
"data" => $b64
]]
];

$context = [
'http' => [
'method'=> 'POST',
'header'=> "Content-type: application/json; charset=UTF-8\r\nx-api-key: $KEY",
'content' => json_encode($data)
]
];

$r = file_get_contents($URL, false, stream_context_create($context));

print($r);

# 実行結果の例:
# {"version": "20201002_180604020502", "outputs": [{"name": "y'", "data": "0.9957193732261658, 0.004280652850866318"}]}
最終更新日時: 2020年 10月 4日(Sunday) 23:32