PHP
下单: OrderCreate
//====Setting Begin=============================
$user_name = 'USER NAME';
$cert = 'API CERT';
$call_name = 'OrderCreate';
$param = array('phone_number' => '13.....', 'product_id' => 1);
$uri = 'THE URI';
//====Setting End===============================
$timestamp = time();
$signature = md5($timestamp . $cert);
$header = array(
'API-USER-NAME: ' . $user_name,
'API-NAME: ' . $call_name,
'API-TIMESTAMP: ' . $timestamp,
'API-SIGNATURE: ' . $signature
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
print_r(json_decode($result));
查单: OrderQuery
//====Setting Begin=============================
$user_name = 'USER NAME';
$cert = 'API CERT';
$call_name = 'OrderQuery';
$param = array('order_number' => 'CZ......');
$uri = 'THE URI';
//====Setting End===============================
$timestamp = time();
$signature = md5($timestamp . $cert);
$header = array(
'API-USER-NAME: ' . $user_name,
'API-NAME: ' . $call_name,
'API-TIMESTAMP: ' . $timestamp,
'API-SIGNATURE: ' . $signature
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
print_r(json_decode($result));
产品列表: ProductQuery
//====Setting Begin=============================
$user_name = 'USER NAME';
$cert = 'API CERT';
$call_name = 'ProductQuery';
$param = array();
$uri = 'THE URI';
//====Setting End===============================
$timestamp = time();
$signature = md5($timestamp . $cert);
$header = array(
'API-USER-NAME: ' . $user_name,
'API-NAME: ' . $call_name,
'API-TIMESTAMP: ' . $timestamp,
'API-SIGNATURE: ' . $signature
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
print_r(json_decode($result));
结果回调: OrderNotify
$user_name = '';
$cert = '';
$call_name = 'OrderNotify';
$headers = apache_request_headers();
//检查必要数据
if ($_SERVER['REQUEST_METHOD'] == 'POST'
&& isset($headers['API-USER-NAME']) && $headers['API-USER-NAME'] == $user_name
&& isset($headers['API-NAME']) && $headers['API-NAME'] == $call_name
&& isset($headers['API-TIMESTAMP']) && isset($headers['API-SIGNATURE']))
{
//验证推送者真实身份
if ($headers['API-SIGNATURE'] == md5($headers['API-TIMESTAMP'] . $cert))
{
$order_number = $_POST['order_number']; //订单号
$shipping_status = $_POST['shipping_status']; //发货状态值
$shipping_status_desc = $_POST['shipping_status_desc']; //发货状态的描述
$shipping_status_message = $_POST['shipping_status_message']; //发货错误的描述
}
}
Java
下单(OrderCreate);查单(OrderQuery);产品列表(ProductQuery)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
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.protocol.HTTP;
public class APICaller {
public static final String user_name = "USER NAME";
public static final String cert = "API CERT";
public static final String uri = "THE URI";
public void orderCreate() {
String call_name = "OrderCreate";
long timestamp = System.currentTimeMillis()/1000L;
String signature = getMD5Str(timestamp + cert);
HttpPost post = new HttpPost(uri);
post.setHeader("API-USER-NAME", user_name);
post.setHeader("API-NAME",call_name);
post.setHeader("API-TIMESTAMP", timestamp + "");
post.setHeader("API-SIGNATURE", signature);
List<NameValuePair> param = new ArrayList <NameValuePair>();
param.add(new BasicNameValuePair("phone_number", "134..."));
param.add(new BasicNameValuePair("product_id", "1"));
try {
post.setEntity(new UrlEncodedFormEntity(param, HTTP.UTF_8));
System.out.println(execute(post));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void orderQuery() {
String call_name = "OrderQuery";
long timestamp = System.currentTimeMillis()/1000L;
String signature = getMD5Str(timestamp + cert);
HttpPost post = new HttpPost(uri);
post.setHeader("API-USER-NAME", user_name);
post.setHeader("API-NAME",call_name);
post.setHeader("API-TIMESTAMP", timestamp + "");
post.setHeader("API-SIGNATURE", signature);
List<NameValuePair> param = new ArrayList <NameValuePair>();
param.add(new BasicNameValuePair("order_number", "LLW..."));
try {
post.setEntity(new UrlEncodedFormEntity(param, HTTP.UTF_8));
System.out.println(execute(post));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void productQuery() {
String call_name = "ProductQuery";
long timestamp = System.currentTimeMillis()/1000L;
String signature = getMD5Str(timestamp + cert);
HttpPost post = new HttpPost(uri);
post.setHeader("API-USER-NAME", user_name);
post.setHeader("API-NAME",call_name);
post.setHeader("API-TIMESTAMP", timestamp + "");
post.setHeader("API-SIGNATURE", signature);
List<NameValuePair> param = new ArrayList <NameValuePair>();
try {
post.setEntity(new UrlEncodedFormEntity(param, HTTP.UTF_8));
System.out.println(execute(post));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private String execute(HttpPost post) throws IOException{
try {
HttpClient http_client = new DefaultHttpClient();
HttpResponse response = http_client.execute(post);
if(response.getStatusLine().getStatusCode() == 404){
throw new IOException("Network Error");
};
InputStream is = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
return sb.toString();
} catch (IOException e) {
throw new IOException("Network Error");
}
}
private String getMD5Str(String str) {
MessageDigest message_digest = null;
try {
message_digest = MessageDigest.getInstance("MD5");
message_digest.reset();
message_digest.update(str.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
System.exit(-1);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
byte[] byte_array = message_digest.digest();
StringBuffer md5_str_buff = new StringBuffer();
for (int i = 0; i < byte_array.length; i++) {
if (Integer.toHexString(0xFF & byte_array[i]).length() == 1) {
md5_str_buff.append("0").append(Integer.toHexString(0xFF & byte_array[i]));
} else {
md5_str_buff.append(Integer.toHexString(0xFF & byte_array[i]));
}
}
return md5_str_buff.toString();
}
}
回调数据接收
package util;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class Utils {
// 获取http请求参数
public static Map getHeaderParams(HttpServletRequest request) {
// 保存参数值
Map paramMap = new HashMap();
if (request != null) {
Enumeration en = request.getHeaderNames();
while (en.hasMoreElements()) {
String paramName = (String) en.nextElement();
paramMap.put(paramName, request.getHeader(paramName));
}
}
return paramMap;
}
// 获取http请求参数
public static Map acquisitionParameters(HttpServletRequest request) {
// 保存参数值
Map paramMap = new HashMap();
if (request != null) {
Enumeration en = request.getParameterNames();
while (en.hasMoreElements()) {
String paramName = (String) en.nextElement();
paramMap.put(paramName, request.getParameter(paramName));
}
}
return paramMap;
}
public static Map getBodyMap(HttpServletRequest request){
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items;
Map param = new HashMap();
try {
items = upload.parseRequest(request);
for(Object object:items){
FileItem fileItem = (FileItem) object;
if (fileItem.isFormField()) {
try {
param.put(fileItem.getFieldName(), fileItem.getString("utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
return param;
}
}
.Net
Note
以下demo代码使用VB.NET编写,.NET系列语言雷同,请亲们酌情参考
下单: OrderCreate
Dim uri As String = "THE URI"
Dim api_user_name As String = "API USER NAME"
Dim api_cert As String = "API CERT"
Dim api_name As String = "OrderCreate"
Dim post_data As String = "phone_number=136xxxxx&product_id=1"
'-------以上是设置--------
Dim data As Byte() = Encoding.UTF8.GetBytes(post_data)
Dim diff As Long = DateDiff("s", "1970-01-01 08:00:00", Now)
Dim timestamp As String = diff.ToString
Dim request As System.Net.HttpWebRequest = CType(System.Net.WebRequest.Create(uri), System.Net.HttpWebRequest)
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
request.Headers.Add("API-USER-NAME", api_user_name)
request.Headers.Add("API-NAME", api_name)
request.Headers.Add("API-TIMESTAMP", timestamp)
request.Headers.Add("API-SIGNATURE", MD5(timestamp & api_cert, 32))
request.ContentLength = data.Length
Dim request_stream As Stream = request.GetRequestStream
request_stream.Write(data, 0, data.Length)
request_stream.Close()
Dim response As System.Net.HttpWebResponse = request.GetResponse()
Dim response_stream As New StreamReader(response.GetResponseStream)
Dim result As String = response_stream.ReadToEnd
Console.WriteLine(result)
查单: OrderQuery
Dim uri As String = "THE URI"
Dim api_user_name As String = "API USER NAME"
Dim api_cert As String = "API CERT"
Dim api_name As String = "OrderQuery"
Dim post_data As String = "order_number=xxx"
'-------以上是设置--------
Dim data As Byte() = Encoding.UTF8.GetBytes(post_data)
Dim diff As Long = DateDiff("s", "1970-01-01 08:00:00", Now)
Dim timestamp As String = diff.ToString
Dim request As System.Net.HttpWebRequest = CType(System.Net.WebRequest.Create(uri), System.Net.HttpWebRequest)
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
request.Headers.Add("API-USER-NAME", api_user_name)
request.Headers.Add("API-NAME", api_name)
request.Headers.Add("API-TIMESTAMP", timestamp)
request.Headers.Add("API-SIGNATURE", MD5(timestamp & api_cert, 32))
request.ContentLength = data.Length
Dim request_stream As Stream = request.GetRequestStream
request_stream.Write(data, 0, data.Length)
request_stream.Close()
Dim response As System.Net.HttpWebResponse = request.GetResponse()
Dim response_stream As New StreamReader(response.GetResponseStream)
Dim result As String = response_stream.ReadToEnd
Console.WriteLine(result)
产品列表: ProductQuery
Dim uri As String = "THE URI"
Dim api_user_name As String = "API USER NAME"
Dim api_cert As String = "API CERT"
Dim api_name As String = "ProductQuery"
Dim post_data As String = ""
'-------以上是设置--------
Dim data As Byte() = Encoding.UTF8.GetBytes(post_data)
Dim diff As Long = DateDiff("s", "1970-01-01 08:00:00", Now)
Dim timestamp As String = diff.ToString
Dim request As System.Net.HttpWebRequest = CType(System.Net.WebRequest.Create(uri), System.Net.HttpWebRequest)
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
request.Headers.Add("API-USER-NAME", api_user_name)
request.Headers.Add("API-NAME", api_name)
request.Headers.Add("API-TIMESTAMP", timestamp)
request.Headers.Add("API-SIGNATURE", MD5(timestamp & api_cert, 32))
request.ContentLength = data.Length
Dim request_stream As Stream = request.GetRequestStream
request_stream.Write(data, 0, data.Length)
request_stream.Close()
Dim response As System.Net.HttpWebResponse = request.GetResponse()
Dim response_stream As New StreamReader(response.GetResponseStream)
Dim result As String = response_stream.ReadToEnd
Console.WriteLine(result)