前言
本文章用于快速接入小蓝本的OPEN API。
开通接入OPEN API前,需要向客户经理确认接入的token; 该token需要在每次请求时放入到header中(xlbak: 你的token)
同时将你们的企业简称全拼写放入到header的user-agent中,以便后续定位问题时可以快速定位
接入方式
系统支持GET与POST方式接入,我们建议使用POST方式
GET请求方式
根据接口文档,将接口中必须的参数通过URLEncoder后连接在接口地址?号后
CURL示例:
curl -H "xlbak: 你的token" "https://open.xiaolanben.com/custom/enniu/companysearch?name=%E6%B7%B1%E5%9C%B3%E5%B0%8F%E8%93%9D%E6%9C%AC%E7%BD%91%E7%BB%9C%E6%8A%80%E6%9C%AF%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8"
PHP示例:
<?php
function httpGet($url, $data){
$curl = curl_init();
$fullUrl = $url;
if(is_array($data)){
$fullUrl = $url ."?". http_build_query($data);
}
echo $fullUrl;
curl_setopt($curl, CURLOPT_URL, $fullUrl);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER,array(
'xlbak: 你的token'
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($curl);
$errorno = curl_errno($curl);
if ($errorno) {
return $errorno;
}
curl_close($curl);
return $res;
}
$data = array("name" => "深圳小蓝本网络技术有限公司");
$resp = httpGet('https://open.xiaolanben.com/custom/enniu/companysearch', $data);
echo $resp;
?>
JAVA示例
@Test
public void get() {
String url = "https://open.xiaolanben.com/custom/enniu/companysearch";
Map<String, String> map = new HashMap<>();
map.put("name", "深圳小蓝本网络技术有限公司");
String queryString = map.entrySet().stream().filter(it -> Objects.nonNull(it.getValue()))
.map(it -> {
try {
return it.getKey() + "=" + URLEncoder.encode(it.getValue(), StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("URL参数编码出错", e);
}
})
.collect(Collectors.joining("&"));
url = url + "?" + queryString;
OkHttpClient httpClient = this.getHttpClient();
Request request = new Request.Builder().url(url)
.header("xlbak", 你的token).get().build();
try (Response resp = httpClient.newCall(request).execute()) {
String strBody = null;
ResponseBody respBody = resp.body();
if (Objects.nonNull(respBody)) {
try (InputStream stream = respBody.byteStream()) {
strBody = IOUtils.toString(stream, StandardCharsets.UTF_8);
}
}
if (!resp.isSuccessful()) {
throw new RuntimeException("http响应码" + resp.code() + ",响应内容" + strBody);
}
System.out.println("调用成功" + strBody);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
POST请求方式
根据接口文档,将接口中必须的参数组装成json结构,通过BODY提交到接口地址
CURL示例:
curl -X POST -H "xlbak: 你的token" -H "Content-Type: application/json" --data '{"name":"深圳小蓝本网络技术有限公司"}' "https://open.xiaolanben.com/custom/enniu/companysearch"
PHP示例:
<?php
function postJson($url, $data){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
if(!$data){
return 'data is null';
}
if(is_array($data)){
$data = json_encode($data);
}
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER,array(
'xlbak: 你的token',
'Content-Type: application/json; charset=utf-8',
'Content-Length:' . strlen($data)
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($curl);
$errorno = curl_errno($curl);
if ($errorno) {
return $errorno;
}
curl_close($curl);
return $res;
}
$data = array("name" => "深圳小蓝本网络技术有限公司");
$resp = postJson('https://open.xiaolanben.com/custom/enniu/companysearch', $data);
echo $resp;
?>
JAVA示例
@Test
public void post() {
String postJson = "{\"name\":\"深圳小蓝本网络技术有限公司\"}";
OkHttpClient httpClient = this.getHttpClient();
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=UTF-8"), postJson);
Request request = new Request.Builder().url("https://open.xiaolanben.com/custom/enniu/companysearch")
.header("xlbak", 你的token).post(body).build();
try (Response resp = httpClient.newCall(request).execute()) {
String strBody = null;
ResponseBody respBody = resp.body();
if (Objects.nonNull(respBody)) {
try (InputStream stream = respBody.byteStream()) {
strBody = IOUtils.toString(stream, StandardCharsets.UTF_8);
}
}
if (!resp.isSuccessful()) {
throw new RuntimeException("http响应码" + resp.code() + ",响应内容" + strBody);
}
System.out.println("调用成功" + strBody);
} catch (IOException e) {
throw new RuntimeException(e);
}
}