开发接口文档
优质
小牛编辑
161浏览
2023-12-01
1. PHP开发接口文档
1.1 发送消息
发送文本、图片、语音消息到小能服务器。
备注:http请求方式为POST方式,格式为XML。
使用样例:
class SendMsg{
function send($message){
//发送消息地址
$url = "https://thirdparty.ntalker.com/wechat";
$header[] = "Content-type: text/xml";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
curl_exec($ch);
curl_close($ch);
}
}
1.2 接收文本消息
接收小能服务器发送过来的消息。
备注:http请求方式为POST方式,格式为JSON。
使用样例:
class msgDemo{
function doPost(){
$msg=file_get_contents("php://input");
echo $msg;
}
}
1.3 接收图片或语音
接收小能服务器发送的图片、语音消息。
备注:Content-Type=multipart/form-data 和 Content-Disposition: form-data;name="file";filename=""
使用样例:
class UploadDemo{
function doPost(){
$file = $_FILES;
//设置文件存放路径
$filedir= "./application/static/uploadfile/";
foreach ($file as $pImg) {
if ($pImg['error'] == UPLOAD_ERR_OK) {
//设置文件名
$filename = iconv('utf-8','gbk', $pImg['name']);
//保存文件
move_uploaded_file($pImg['tmp_name'], $filedir.$filename);
}
}
echo "{\"type\":\"TYPE\",\"media_id\":\"MEDIA_ID\",\"created_at\":123456789}";
}
}
2. JAVA开发接口文档
2.1 发送消息
发送文本、图片、语音消息到小能服务器。
备注:http请求方式为POST方式,格式为XML。
使用样例:
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class SendMsg {
/**
* 发送消息给小能在线客服
* @param message 文本、图片、语音消息
*/
public void sendMsg(String message) {
String http = "https://thirdparty.ntalker.com/wechat";
HttpURLConnection httpConn = null;
try {
URL url = new URL(http);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestProperty("Content-Type", "text/xml");
httpConn.setRequestProperty("User-agent", "MSIE8.0");
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setReadTimeout(5000);
httpConn.setConnectTimeout(5000);
OutputStream op = httpConn.getOutputStream();
op.write(message.getBytes("utf-8"));
op.flush();
op.close();
if (httpConn.getResponseCode() == 200){
System.out.println("success");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpConn != null)
httpConn.disconnect();
}
}
}
2.2 接收文本消息
接收小能服务器发送过来的消息。
备注:http请求方式为POST方式,格式为JSON。
使用样例:
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.q2.util.Statics;
public class DemoServlet extends HttpServlet {
/**
* 接收JSON消息
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
InputStream ips = request.getInputStream();
String param = ips.toString();
System.out.println(param);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.3 接收图片或语音
接收小能服务器发送的图片、语音消息。
备注:Content-Type=multipart/form-data 和 Content-Disposition: form-data;name="file";filename=""
使用样例:
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* 接收图片、语音消息
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
//写入目录
String showFileName = "C:\\Users\\Administrator\\Desktop";
try {
String type = request.getParameter("type");
System.out.println(type);
//获取所有的文件集合
List<FileItem> fileItemList = servletFileUpload.parseRequest(request);
Iterator<FileItem> iterator = fileItemList.iterator();
while(iterator.hasNext()){
FileItem fileItem = (FileItem)iterator.next();
//文件名
String fileName = fileItem.getName();
//写入文件路径
String filePath = showFileName + "\\" + fileName;
File saveFile = new File(filePath);
fileItem.write(saveFile);
//HTTP返回
System.out.println("上传成功");
PrintWriter out = response.getWriter();
out.print("{\"type\":\"TYPE\",\"media_id\":\"MEDIA_ID\",\"created_at\":123456789}");
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}