前言
本文章用于快速接入小蓝本的OPEN API。
在接入前请登录 小蓝本开放平台,并至个人中心->我的接口(按版本:标准版 / 定制版)中获取accessId与accessToken。
在接口调用时,需将该两个参数放入到每个请求的header中
注意:所有请求均不可带入名为 X-Forwarded-For 与 X-Remote-IP 的header信息
接入方式
根据接口定义不同,将会用到对应的请求方法:GET或POST,详见具体接口的定义
GET请求方式
根据接口文档,将接口中必须的参数通过URLEncoder后连接在接口地址?号后
CURL示例:
curl -H "accessId: 你的accessId" -H "accessToken: 你的token" "https://openapi.xiaolanben.com/api/1001?eid=qfef52d62a2c2f1347a52e47a5fc01d97"
PHP示例:
<?php
function httpGet($url, $data){
$curl = curl_init();
$fullUrl = $url;
if(is_array($data)){
$fullUrl = $url ."?". http_build_query($data);
}
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(
'accessId: 你的accessId',
'accessToken: 你的accessToken'
));
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("eid" => "qfef52d62a2c2f1347a52e47a5fc01d97");
$resp = httpGet('https://openapi.xiaolanben.com/api/1001', $data);
echo $resp;
?>
JAVA示例
@Test
public void get() {
String url = "https://openapi.xiaolanben.com/api/1001";
Map<String, String> map = new HashMap<>();
map.put("eid", "qfef52d62a2c2f1347a52e47a5fc01d97");
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("accessId", "你的accessId").header("accessToken", "你的accessToken").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 "accessId: 你的accessId" -H "accessToken: 你的token" -H "Content-Type: application/json" --data '{"eid":"qfef52d62a2c2f1347a52e47a5fc01d97"}' "https://openapi.xiaolanben.com/api/1001"
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(
'accessId: 你的accessId',
'accessToken: 你的accessToken',
'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("eid" => "qfef52d62a2c2f1347a52e47a5fc01d97");
$resp = postJson('https://openapi.xiaolanben.com/api/1001', $data);
echo $resp;
?>
JAVA示例
@Test
public void post() {
String postJson = "{\"eid\":\"qfef52d62a2c2f1347a52e47a5fc01d97\"}";
OkHttpClient httpClient = this.getHttpClient();
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=UTF-8"), postJson);
Request request = new Request.Builder().url("https://openapi.xiaolanben.com/api/1001")
.header("accessId", "你的accessId").header("accessToken", "你的accessToken").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);
}
}
JAVA示例使用包与公共代码
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.3.0</version>
</dependency>
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import okhttp3.OkHttpClient.Builder;
/**
* 构建http连接
*/
public OkHttpClient getHttpClient() {
ConnectionPool CONNECTION_POOL = new ConnectionPool(1, 5L, TimeUnit.MINUTES);
Builder builder = new Builder().connectionPool(CONNECTION_POOL) //按自己定制连接池
.connectTimeout(3000, TimeUnit.MILLISECONDS) //按实际定义连接超时
.writeTimeout(3000, TimeUnit.MILLISECONDS) //按实际定义写超时
.readTimeout(5000, TimeUnit.MILLISECONDS); //按实际定义读超时
try {
initHttpBuilder4SSL(builder);
} catch (Exception e) {
throw new RuntimeException("服务初始化ssl支持出错", e);
}
return builder.build();
}
/**
* 初始化SSL
*/
private void initHttpBuilder4SSL(OkHttpClient.Builder httpBuilder)
throws NoSuchAlgorithmException, KeyManagementException {
X509TrustManager trustManager = new X509TrustManager(){
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
};
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{trustManager}, null);
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
httpBuilder.sslSocketFactory(sslSocketFactory, trustManager);
httpBuilder.hostnameVerifier((hostname, session) -> true);
}