1:N 搜索

优质
小牛编辑
129浏览
2023-12-01

1.接口描述

该API的功能是创建一个1比 n 图片搜索库。

  • 每个库最多图片上限5000张,每个api_id最多建立5个图片搜索库。

请求方式

POST

请求 URL

https://cloudapi.linkface.cn/search/db/create

2.请求参数

字段类型必需描述
api_idstringAPI 账户
api_secretstringAPI 密钥
namestring搜索库的名字
descstring备注。对搜索库的描述,长度不超过512

所有中文和特殊字符必需以UTF-8编码转义。

3.返回参数

字段类型说明
request_idstring本次请求的id
statusstring状态,正常为 OK,其他值表示失败,详见错误码

返回样例

{
  "request_id": "TID8bf47ab6eda64476973cc5f5b6ebf57e",
  "status": "OK",
}

4.错误码

错误响应

状态码status 字段说明
400ENCODING_ERROR参数非UTF-8编码
400FDB_ERROR1比n出错,具体原因见 reason 字段内容
400INVALID_ARGUMENT请求参数错误,具体原因见 reason 字段内容
401UNAUTHORIZED账号或密钥错误
401KEY_EXPIRED账号过期,具体情况见 reason 字段内容
403RATE_LIMIT_EXCEEDED调用频率超出限额
403NO_PERMISSION无调用权限
403OUT_OF_QUOTA调用次数超出限额
404NOT_FOUND请求路径错误
500INTERNAL_ERROR服务器内部错误

返回样例

{
  "status": "INVALID_ARGUMENT",
  "reason": "argument 'name': not found",
  "request_id": "TID8bf47ab6eda64476973cc5f5b6ebf57e"
}

5.输入示例

  • cURL 样例
curl -X POST "https://cloudapi.linkface.cn/search/db/create?api_id=ID&api_secret=SECRET" \
  -F name=db
  • HTTPie 样例
http -f POST "https://cloudapi.linkface.cn/search/db/create?api_id=ID&api_secret=SECRET" \
  name=db
  • C++ 样例
#include <iostream>
#include <exception>
#include <cstring>
#include <curl/curl.h>
#include <json/json.h>
using namespace std;

size_t callback(char *ptr, size_t size, size_t nmemb, string &stream){
  size_t sizes = size*nmemb;
  string temp(ptr,sizes);
  stream += temp;
  return sizes;
}

int main( int argv, char * argc[] ){

  CURL *curl;
  CURLcode res;
  curl_slist *list;
  string stream;
  long code;
  // set params
  Json::Value params;
  params["api_id"] = "ID";
  params["api_secret"] = "SECRET";
  params["name"] = "db";
  string data = params.toStyledString();
  try
  {
    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();

    if( curl ){

      list = curl_slist_append(NULL,"Content-Type:application/json;charset=UTF-8");

      curl_easy_setopt(curl, CURLOPT_URL, "https://cloudapi.linkface.cn/search/db/create");
      curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);  
      curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
      curl_easy_setopt(curl, CURLOPT_WRITEDATA, &stream);
      //skip verification
      #ifdef SKIP_PEER_VERIFICATION
          curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
      #endif

      #ifdef SKIP_HOSTNAME_VERIFICATION
          curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
      #endif

      res = curl_easy_perform(curl);
      // perform failed
      if( res != CURLE_OK ){
        cout<<"curl_easy_perform() failed:"<<curl_easy_strerror(res)<<endl;
        return -1;
      }
      // HTTP Status Code
      curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
      // parse json  
      Json::Reader *reader = new Json::Reader(Json::Features::strictMode());
      Json::Value res_json;
      if(!reader->parse(stream, res_json)){
            cout<<"parse error";
            return -1;
      }
      cout<<"HTTP Status Code: "<<code<<endl;
      cout<<res_json<<endl;
      // End the libcurl easy handle
      curl_easy_cleanup(curl);   
    }
    curl_global_cleanup();
  }catch(exception ex){
    cout<<"curl exception "<<ex.what()<<endl;
  }
  return 0;
}
  • Java 样例
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; 

public class httpClientPost { 
    public static final String api_id = "ID"; 
    public static final String api_secret = "SECRET";
    public static final String name = "db";
    public static final String POST_URL = 
        "https://cloudapi.linkface.cn/search/db/create";

    public static void HttpClientPost() {
        HttpClient httpClient = new DefaultHttpClient();
        String url = POST_URL;
        HttpPost httpPost = new HttpPost(url);
        NameValuePair pair1 = new BasicNameValuePair("api_id", api_id);
        NameValuePair pair2 = new BasicNameValuePair("api_secret", api_secret);
        NameValuePair pair3 = new BasicNameValuePair("name", name);
        ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
        pairs.add(pair1);
        pairs.add(pair2);
        pairs.add(pair3);
        try {
            HttpEntity requestEntity = new UrlEncodedFormEntity(pairs);
            httpPost.setEntity(requestEntity);
            try{
                HttpResponse response = httpClient.execute(httpPost);
                if (response.getStatusLine().getStatusCode() == 200) {
                    HttpEntity entity = response.getEntity();
                    BufferedReader reader = new BufferedReader(
                        new InputStreamReader(entity.getContent()));
                    String line = reader.readLine();
                    System.out.println(line);
                }else{
                    HttpEntity r_entity = response.getEntity();
                    String responseString = EntityUtils.toString(r_entity);
                    System.out.println("错误码是:"+response.getStatusLine().getStatusCode()+"  "+response.getStatusLine().getReasonPhrase());
                    System.out.println("出错原因是:"+responseString);
                    //你需要根据出错的原因判断错误信息,并修改
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
           try {
               HttpClientPost();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • Ruby 样例
require 'net/http'
require 'json'

data = {
  'api_id' => 'ID',
  'api_secret' => 'SECRET',
  'name' => 'db'
}.to_json
begin
  uri = URI.parse('https://cloudapi.linkface.cn/search/db/create')
  req = Net::HTTP::Post.new(uri.request_uri)
  # Request headers
  req['Content-Type'] = 'application/json'
  # Request body
  req.body = "#{data}"

  res = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
    http.request(req)
  end
  result = JSON.parse(res.body)
  puts result
rescue Exception => e
  puts e.message
end
  • Python 样例

我们提供的样例支持 Python 2.7 & 3.4–3.7 和 PyPy,其他版本暂不提供,需要您查阅相关资料。

import requests

try:
    host = 'https://cloudapi.linkface.cn'
    url = host + '/search/db/create'
    data = {'api_id': 'ID','api_secret': 'SECRET','name':'DB_NAME'}

    response = requests.post(url, data = data)
    result = response.json()
    print result
except Exception as e:
    print("type error: " + str(e))
  • PHP 样例
<?php  
   $testurl = 'https://cloudapi.linkface.cn/search/db/create';
   $post_data = array ('api_id' => 'ID','api_secret' => 'SECRET',
             'name' => 'db');
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $testurl);  
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//您可以根据需要,决定是否打开SSL验证
   curl_setopt($ch, CURLOPT_POST,1);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
   $output = curl_exec($ch); 
   var_dump($output);  
   curl_close($ch); 
?>