# 服务概览
# 服务介绍
图片内容鉴定服务是新浪开发的图片内容深度挖掘工具,提供涉政、色情、暴恐、营业执照识别等内容识别。
# 服务使用
云应用 (opens new window) 默认支持服务且默认已经开启,使用云应用的accesskey和secretkey签名调用该服务提供的API即可。
# 服务价格
从https://www.sinacloud.com/index/price.html (opens new window)服务价格页面查看 图片鉴定服务 即可查询服务单价,按请求次数计费,鉴定服务异步完成,支持回调可通过接口查询任务状态,添加任务、查询任务状态不算调用次数。
# 接口地址
服务通过http://g.sae.sina.com.cn
提供服务,接口可以从非新浪云的环境调用,需要使用云应用的accesskey和secretkey按以下说明的签名规则后才可以调用。
# 签名规则
将所有x-sae-
开头的http header
(小写)按name排序后用冒号链接name和value,放在method、uri(包含query string,不要转义)后以换行符分隔拼接为签名原文字符串,再以Secretkey为密钥用hmac sha256计算哈希值(二进制),再经base64编码,前面加上"SAEV1_HMAC_SHA256 "
,得到本次请求的认证签名,即Authorization字段。
# 认证的请求头
请求头 | 说明 |
---|---|
x-sae-accesskey | 应用的Accesskey |
x-sae-timestamp | Unix时间戳 |
Authorization | 本次请求的认证签名 |
# 请求头示例
GET /log/http/2015-06-05/1-access.log HTTP/1.1
Host: g.sae.sina.com.cn
Accept: text/plain
x-sae-accesskey: 0xdeadbeef
x-sae-timestamp: 1433495016
Authorization: SAEV1_HMAC_SHA256 VQdy0s/D4aEn6QXAexr0onGmz+QObDwkWcZCCKcxMws=
# Python签名示例
import time
import base64
import hmac
import hashlib
TIMESTAMP = str(int(time.time()))
URI = '/log/http/2015-05-06/1-access.log?head/0/1'
# x-sae-开头的header的name需要是小写的
msgToSign = "\n".join(["GET", URI, "\n".join([(k + ":" + v) for k, v in sorted(HEADERS) if k.startswith('x-sae-')])])
signature = "SAEV1_HMAC_SHA256 " + base64.b64encode(hmac.new(SECRETKEY, msgToSign, hashlib.sha256).digest())
# PHP签名示例
<?php
namespace sinacloud\sae;
class Gapi
{
private $accessKey;
private $secretKey;
private $gapi = 'http://g.sinacloud.com';
public function __construct($accessKey, $secretKey)
{
$this->accessKey = $accessKey;
$this->secretKey = $secretKey;
}
/**
* 发送一个GET请求
* @param $uri
* @return bool|mixed
*/
public function get($uri)
{
if (!$uri) {
return false;
}
return $this->_curl($uri);
}
public function post($uri, $post_data = array())
{
if (!$uri) {
return false;
}
return $this->_curl($uri, $post_data, 'POST');
}
private function _cal_sign_and_set_header($ch, $uri, $method = 'GET')
{
$a = array();
$a[] = $method;
$a[] = $uri;
// $timeline unix timestamp
$timeline = time();
$b = array('x-sae-accesskey' => $this->accessKey, 'x-sae-timestamp' => $timeline);
ksort($b);
foreach ($b as $key => $value) {
$a[] = sprintf("%s:%s", $key, $value);
}
$str = implode("\n", $a);
$s = hash_hmac('sha256', $str, $this->secretKey, true);
$b64_s = base64_encode($s);
$headers = array();
$headers[] = sprintf('x-sae-accesskey:%s', $this->accessKey);
$headers[] = sprintf('x-sae-timestamp:%s', $timeline);
$headers[] = sprintf('Authorization: SAEV1_HMAC_SHA256 %s', $b64_s);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
return $headers;
}
private function _curl($uri, $post_data = array(), $method = 'GET')
{
$ch = curl_init();
$url = sprintf('%s%s', $this->gapi, $uri);
curl_setopt($ch, CURLOPT_URL, $url);
$this->_cal_sign_and_set_header($ch, $uri, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($post_data) {
if (is_array($post_data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
} else {
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
}
$txt = curl_exec($ch);
$error = curl_errno($ch);
curl_close($ch);
if ($error) {
return false;
}
return $txt;
}
}
# 接口返回码说明
状态码 | 说明 |
---|---|
200 OK | 无错误 |
403 Forbidden | 认证失败 |
404 Not Found | 接口不存在等 |
405 Method Not Allowed | 不支持的http method |
# 查询任务状态
# 接口说明
调用图片内容鉴定服务接口后,接口会返回一个任务的id,任务异步完成,可以通过返回的任务id接口查询任务的当前执行状态。
# 接口地址
http://g.sae.sina.com.cn/pictureidentificate/task/status (opens new window)
# 签名
需要签名,请参考签名规则章节说明。
# 请求方式
POST
# 请求参数
参数 | 是否必选 | 说明 | 示例 |
---|---|---|---|
task_id | 是 | 查询的任务ID | 7385589 |
# 返回结果示例
{
"code": 0,
"message": "success",
"data": {
"Id": 14074694,
"RunAt": "2018-12-12T21:10:27+08:00",
"UpdateAt": "2018-12-12T21:10:39+08:00",
"ErrorCount": 0,
"MaxRetry": 2,
"LastError": "",
"Result": "{\"code\":0,\"message\":\"success\",\"data\":{\"code\":0,\"desc\":\"\",\"result\":{\"rows\":[{\"data\":{\"result\":1,\"score\":0.92569363117218,\"label\":2,\"name\":\"porn\"},\"type\":\"porn4\"}]}}}",
"Status": "DONE"
}
}
其中:
- Id:异步任务的ID
- RunAt:任务的执行时间
- UpdateAt:最后更新的时间
- ErrorCount:出错的次数
- MaxRetry:任务最大的重试次数
- LastError:最后的错误,为空表示没有错误
- Result:任务输出的结果
- Status:当前任务的状态,PENDING表示等待执行、RUNNING表示任务正在执行、DONE表示任务执行成功、FAILED表示任务执行失败
# PHP调用demo
<?php
$id = '你的任务编号';
require('gapi.php');
use sinacloud\sae\Gapi;
$i = new Gapi('云应用的accesskey', '云应用的secretkey');
$post_data = array();
$post_data['task_id'] = $id;
$ret = $i->post('/pictureidentificate/task/status', $post_data);
echo($ret);
其中gapi.php
的内容如下:
<?php
namespace sinacloud\sae;
class Gapi
{
private $accessKey;
private $secretKey;
private $gapi = 'http://g.sinacloud.com';
public function __construct($accessKey, $secretKey)
{
$this->accessKey = $accessKey;
$this->secretKey = $secretKey;
}
/**
* 发送一个GET请求
* @param $uri
* @return bool|mixed
*/
public function get($uri)
{
if (!$uri) {
return false;
}
return $this->_curl($uri);
}
public function post($uri, $post_data = array())
{
if (!$uri) {
return false;
}
return $this->_curl($uri, $post_data, 'POST');
}
private function _cal_sign_and_set_header($ch, $uri, $method = 'GET')
{
$a = array();
$a[] = $method;
$a[] = $uri;
// $timeline unix timestamp
$timeline = time();
$b = array('x-sae-accesskey' => $this->accessKey, 'x-sae-timestamp' => $timeline);
ksort($b);
foreach ($b as $key => $value) {
$a[] = sprintf("%s:%s", $key, $value);
}
$str = implode("\n", $a);
$s = hash_hmac('sha256', $str, $this->secretKey, true);
$b64_s = base64_encode($s);
$headers = array();
$headers[] = sprintf('x-sae-accesskey:%s', $this->accessKey);
$headers[] = sprintf('x-sae-timestamp:%s', $timeline);
$headers[] = sprintf('Authorization: SAEV1_HMAC_SHA256 %s', $b64_s);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
return $headers;
}
private function _curl($uri, $post_data = array(), $method = 'GET')
{
$ch = curl_init();
$url = sprintf('%s%s', $this->gapi, $uri);
curl_setopt($ch, CURLOPT_URL, $url);
$this->_cal_sign_and_set_header($ch, $uri, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($post_data) {
if (is_array($post_data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
} else {
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
}
$txt = curl_exec($ch);
$error = curl_errno($ch);
curl_close($ch);
if ($error) {
return false;
}
return $txt;
}
}