JavaScript (Fetch API)
// 搜索术语
const searchTerms = async (query) => {
const response = await fetch('/api/v1/terms/search?q=' + encodeURIComponent(query), {
headers: {
'X-API-Key': 'your_api_key_here'
}
});
const data = await response.json();
return data;
};
// 术语匹配
const matchTerms = async (text) => {
const response = await fetch('/api/v1/match', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': 'your_api_key_here'
},
body: JSON.stringify({ text: text })
});
const data = await response.json();
return data;
};
PHP (cURL)
<?php
// 搜索术语
function searchTerms($query, $apiKey) {
$url = '/api/v1/terms/search?q=' . urlencode($query);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'X-API-Key: ' . $apiKey
]);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
// 术语匹配
function matchTerms($text, $apiKey) {
$url = '/api/v1/match';
$data = json_encode(['text' => $text]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'X-API-Key: ' . $apiKey
]);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
?>
Python (requests)
import requests
import json
class TermsAPI:
def __init__(self, base_url, api_key):
self.base_url = base_url
self.headers = {'X-API-Key': api_key}
def search_terms(self, query, category=None, page=1, limit=20):
"""搜索术语"""
params = {'q': query, 'page': page, 'limit': limit}
if category:
params['category'] = category
response = requests.get(
f'{self.base_url}/terms/search',
params=params,
headers=self.headers
)
return response.json()
def match_terms(self, text, include_context=True):
"""术语匹配"""
data = {
'text': text,
'include_context': include_context
}
response = requests.post(
f'{self.base_url}/match',
json=data,
headers=self.headers
)
return response.json()
# 使用示例
api = TermsAPI('http://your-domain.com/api/v1', 'your_api_key_here')
result = api.search_terms('中国梦')
print(result)