一、前期准备

1. 各平台 API 权限申请

平台 申请地址 核心凭证 关键说明
1688 开放平台 https://open.1688.com/ appKey、appSecret 需企业认证,开通商品详情接口
马帮 ERP https://api.mabang.com/ app_id、app_secret 申请采集 / 商品同步接口权限
店小秘 https://open.dianxiaomi.com/ appKey、appSecret 配置 IP 白名单,开通采集接口
芒果采集 联系芒果采集服务商 token/API 密钥、接口地址 第三方工具,接口为定制化

2. 通用依赖(Java/PHP)

  • Java:OkHttp(HTTP 请求)、FastJSON(JSON 解析)、commons-codec(签名)

    xml

    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>4.10.0</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>2.0.32</version>
    </dependency>
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.15</version>
    </dependency>
    
  • PHP:curl(HTTP 请求)、json 扩展(JSON 解析)

二、1688 官方商品采集 API 实现

核心接口:商品详情查询

1688 开放平台核心接口:taobao.open.supply.center.product.detail.get(根据商品 ID / 链接获取详情)

1. Java 实现

java

运行

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.codec.digest.DigestUtils;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.util.*;

public class Ali1688Collector {
    // 1688配置
    private static final String APP_KEY = "你的1688appKey";
    private static final String APP_SECRET = "你的1688appSecret";
    private static final String API_URL = "https://gw.open.1688.com/openapi/param2/1/taobao.open.supply.center.product.detail.get/"+APP_KEY;

    // 生成1688签名
    private static String generateSign(Map<String, String> params) {
        // 1. 参数按ASCII升序排序
        List<String> keys = new ArrayList<>(params.keySet());
        Collections.sort(keys);
        // 2. 拼接参数字符串
        StringBuilder sb = new StringBuilder();
        for (String key : keys) {
            sb.append(key).append(params.get(key));
        }
        // 3. 首尾拼接appSecret并MD5加密
        sb.insert(0, APP_SECRET);
        sb.append(APP_SECRET);
        return DigestUtils.md5Hex(sb.toString()).toUpperCase();
    }

    // 采集1688商品详情
    public static JSONObject collect1688Product(String productId) throws IOException {
        OkHttpClient client = new OkHttpClient();
        // 构造请求参数
        Map<String, String> params = new HashMap<>();
        params.put("method", "taobao.open.supply.center.product.detail.get");
        params.put("app_key", APP_KEY);
        params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        params.put("format", "json");
        params.put("v", "2.0");
        params.put("sign_method", "md5");
        params.put("product_id", productId); // 1688商品ID

        // 生成签名
        params.put("sign", generateSign(params));

        // 构造请求URL
        StringBuilder urlBuilder = new StringBuilder(API_URL);
        urlBuilder.append("?");
        for (Map.Entry<String, String> entry : params.entrySet()) {
            urlBuilder.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
        }
        String url = urlBuilder.substring(0, urlBuilder.length() - 1);

        // 发送请求
        Request request = new Request.Builder().url(url).get().build();
        Response response = client.newCall(request).execute();

        // 解析返回结果
        if (response.isSuccessful() && response.body() != null) {
            String result = response.body().string();
            return JSONObject.parseObject(result);
        } else {
            throw new IOException("1688 API请求失败:" + response.code());
        }
    }

    public static void main(String[] args) throws IOException {
        JSONObject productInfo = collect1688Product("123456789"); // 替换为真实商品ID
        System.out.println("1688商品信息:" + productInfo.toJSONString());
    }
}

2. PHP 实现

php

运行

<?php
class Ali1688Collector {
    private $appKey = "你的1688appKey";
    private $appSecret = "你的1688appSecret";
    private $apiUrl = "https://gw.open.1688.com/openapi/param2/1/taobao.open.supply.center.product.detail.get/";

    // 生成1688签名
    private function generateSign($params) {
        // 1. 参数按ASCII升序排序
        ksort($params);
        // 2. 拼接参数字符串
        $signStr = '';
        foreach ($params as $key => $value) {
            $signStr .= $key . $value;
        }
        // 3. 首尾拼接appSecret并MD5加密
        $signStr = $this->appSecret . $signStr . $this->appSecret;
        return strtoupper(md5($signStr));
    }

    // 采集1688商品详情
    public function collect1688Product($productId) {
        // 构造请求参数
        $params = [
            'method' => 'taobao.open.supply.center.product.detail.get',
            'app_key' => $this->appKey,
            'timestamp' => date('Y-m-d H:i:s'),
            'format' => 'json',
            'v' => '2.0',
            'sign_method' => 'md5',
            'product_id' => $productId
        ];

        // 生成签名
        $params['sign'] = $this->generateSign($params);

        // 构造请求URL
        $url = $this->apiUrl . $this->appKey . '?' . http_build_query($params);

        // 发送请求
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $response = curl_exec($ch);
        curl_close($ch);

        // 解析结果
        if ($response) {
            return json_decode($response, true);
        } else {
            throw new Exception("1688 API请求失败");
        }
    }
}

// 调用示例
try {
    $collector = new Ali1688Collector();
    $productInfo = $collector->collect1688Product("123456789"); // 替换为真实商品ID
    var_dump("1688商品信息:", $productInfo);
} catch (Exception $e) {
    echo "错误:" . $e->getMessage();
}
?>

三、马帮 ERP API 对接(商品同步)

马帮 API 基于 RESTful 风格,核心是签名验证 + JSON 请求,以下为商品采集同步示例:

1. Java 实现

java

运行

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class MabangApiClient {
    private static final String MABANG_APP_ID = "你的马帮app_id";
    private static final String MABANG_APP_SECRET = "你的马帮app_secret";
    private static final String MABANG_API_URL = "https://api.mabang.com/v1/product/collect"; // 马帮采集接口

    // 马帮API请求
    public static JSONObject collectByMabang(String productUrl) throws IOException {
        OkHttpClient client = new OkHttpClient();
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");

        // 构造请求体
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("app_id", MABANG_APP_ID);
        requestBody.put("timestamp", System.currentTimeMillis() / 1000); // 时间戳(秒)
        requestBody.put("product_url", productUrl); // 1688商品链接

        // 生成签名(马帮签名规则:MD5(app_id+timestamp+app_secret))
        String signStr = MABANG_APP_ID + requestBody.get("timestamp") + MABANG_APP_SECRET;
        String sign = org.apache.commons.codec.digest.DigestUtils.md5Hex(signStr);
        requestBody.put("sign", sign);

        // 构建请求
        RequestBody body = RequestBody.create(JSONObject.toJSONString(requestBody), JSON);
        Request request = new Request.Builder()
                .url(MABANG_API_URL)
                .post(body)
                .build();

        Response response = client.newCall(request).execute();
        if (response.isSuccessful() && response.body() != null) {
            String result = response.body().string();
            return JSONObject.parseObject(result);
        } else {
            throw new IOException("马帮API请求失败:" + response.code());
        }
    }

    public static void main(String[] args) throws IOException {
        JSONObject result = collectByMabang("https://detail.1688.com/offer/123456789.html");
        System.out.println("马帮采集结果:" + result.toJSONString());
    }
}

2. PHP 实现

php

运行

<?php
class MabangApiClient {
    private $appId = "你的马帮app_id";
    private $appSecret = "你的马帮app_secret";
    private $apiUrl = "https://api.mabang.com/v1/product/collect";

    public function collectByMabang($productUrl) {
        // 构造请求参数
        $timestamp = time();
        $sign = md5($this->appId . $timestamp . $this->appSecret);

        $postData = [
            'app_id' => $this->appId,
            'timestamp' => $timestamp,
            'product_url' => $productUrl,
            'sign' => $sign
        ];

        // 发送POST请求
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->apiUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $response = curl_exec($ch);
        curl_close($ch);

        return json_decode($response, true);
    }
}

// 调用示例
$mabangClient = new MabangApiClient();
$result = $mabangClient->collectByMabang("https://detail.1688.com/offer/123456789.html");
var_dump("马帮采集结果:", $result);
?>

四、店小秘 API 对接

店小秘 API 签名规则为 HMAC-SHA256,以下为商品采集示例:

1. Java 实现

java

运行

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import com.alibaba.fastjson.JSONObject;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public class DianXiaoMiApiClient {
    private static final String DXMI_APP_KEY = "你的店小秘appKey";
    private static final String DXMI_APP_SECRET = "你的店小秘appSecret";
    private static final String DXMI_API_URL = "https://open.dianxiaomi.com/api/v2/product/collect";

    // 生成HMAC-SHA256签名
    private static String generateHmacSign(String data) throws Exception {
        Mac mac = Mac.getInstance("HmacSHA256");
        SecretKeySpec keySpec = new SecretKeySpec(DXMI_APP_SECRET.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
        mac.init(keySpec);
        byte[] signBytes = mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(signBytes);
    }

    public static JSONObject collectByDxmi(String productUrl) throws Exception {
        OkHttpClient client = new OkHttpClient();
        long timestamp = System.currentTimeMillis();

        // 构造签名原文
        String signData = DXMI_APP_KEY + timestamp + productUrl;
        String sign = generateHmacSign(signData);

        // 构造请求参数
        Map<String, String> params = new HashMap<>();
        params.put("appKey", DXMI_APP_KEY);
        params.put("timestamp", String.valueOf(timestamp));
        params.put("sign", sign);
        params.put("productUrl", productUrl);

        // 构造URL
        StringBuilder urlBuilder = new StringBuilder(DXMI_API_URL);
        urlBuilder.append("?");
        for (Map.Entry<String, String> entry : params.entrySet()) {
            urlBuilder.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
        }
        String url = urlBuilder.substring(0, urlBuilder.length() - 1);

        Request request = new Request.Builder().url(url).get().build();
        Response response = client.newCall(request).execute();

        if (response.isSuccessful() && response.body() != null) {
            return JSONObject.parseObject(response.body().string());
        } else {
            throw new IOException("店小秘API请求失败:" + response.code());
        }
    }

    public static void main(String[] args) throws Exception {
        JSONObject result = collectByDxmi("https://detail.1688.com/offer/123456789.html");
        System.out.println("店小秘采集结果:" + result.toJSONString());
    }
}

2. PHP 实现

php

运行

<?php
class DianXiaoMiApiClient {
    private $appKey = "你的店小秘appKey";
    private $appSecret = "你的店小秘appSecret";
    private $apiUrl = "https://open.dianxiaomi.com/api/v2/product/collect";

    // 生成HMAC-SHA256签名
    private function generateHmacSign($data) {
        return base64_encode(hash_hmac('sha256', $data, $this->appSecret, true));
    }

    public function collectByDxmi($productUrl) {
        $timestamp = time() * 1000; // 毫秒级时间戳
        $signData = $this->appKey . $timestamp . $productUrl;
        $sign = $this->generateHmacSign($signData);

        // 构造请求参数
        $params = [
            'appKey' => $this->appKey,
            'timestamp' => $timestamp,
            'sign' => $sign,
            'productUrl' => $productUrl
        ];

        $url = $this->apiUrl . '?' . http_build_query($params);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $response = curl_exec($ch);
        curl_close($ch);

        return json_decode($response, true);
    }
}

// 调用示例
$dxmiClient = new DianXiaoMiApiClient();
$result = $dxmiClient->collectByDxmi("https://detail.1688.com/offer/123456789.html");
var_dump("店小秘采集结果:", $result);
?>

五、芒果采集 API 通用对接

芒果采集为第三方工具,接口格式需以服务商提供为准,以下为通用 HTTP 请求示例:

1. Java 实现

java

运行

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class MangoCollector {
    private static final String MANGO_API_URL = "芒果采集API地址";
    private static final String MANGO_TOKEN = "你的芒果采集token";

    public static JSONObject collectByMango(String productUrl) throws IOException {
        OkHttpClient client = new OkHttpClient();

        // 构造请求参数(以服务商要求为准)
        Map<String, String> params = new HashMap<>();
        params.put("token", MANGO_TOKEN);
        params.put("url", productUrl);
        params.put("type", "1688"); // 采集类型

        // 构造URL
        StringBuilder urlBuilder = new StringBuilder(MANGO_API_URL);
        urlBuilder.append("?");
        for (Map.Entry<String, String> entry : params.entrySet()) {
            urlBuilder.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
        }
        String url = urlBuilder.substring(0, urlBuilder.length() - 1);

        Request request = new Request.Builder().url(url).get().build();
        Response response = client.newCall(request).execute();

        if (response.isSuccessful() && response.body() != null) {
            return JSONObject.parseObject(response.body().string());
        } else {
            throw new IOException("芒果采集API请求失败:" + response.code());
        }
    }

    public static void main(String[] args) throws IOException {
        JSONObject result = collectByMango("https://detail.1688.com/offer/123456789.html");
        System.out.println("芒果采集结果:" + result.toJSONString());
    }
}

2. PHP 实现

php

运行

<?php
class MangoCollector {
    private $apiUrl = "芒果采集API地址";
    private $token = "你的芒果采集token";

    public function collectByMango($productUrl) {
        $params = [
            'token' => $this->token,
            'url' => $productUrl,
            'type' => '1688'
        ];

        $url = $this->apiUrl . '?' . http_build_query($params);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $response = curl_exec($ch);
        curl_close($ch);

        return json_decode($response, true);
    }
}

// 调用示例
$mangoClient = new MangoCollector();
$result = $mangoClient->collectByMango("https://detail.1688.com/offer/123456789.html");
var_dump("芒果采集结果:", $result);
?>

六、关键注意事项

  1. 签名正确性:各平台签名规则不同(MD5/HMAC-SHA256),需严格按文档排序参数、拼接密钥,避免签名错误。
  2. 频率限制:1688 / 马帮 / 店小秘均有接口调用限流(如 1688 单应用 QPS=5),需添加限流重试逻辑。
  3. 数据解析:返回结果需处理空值、错误码(如 1688 的error_response),确保程序健壮性。
  4. 合规性:采集 1688 商品数据需遵守平台《开放平台服务协议》,不得用于违规爬取、商用侵权等场景。
  5. 异常处理:添加网络超时、接口返回失败的重试机制,建议使用线程池 / 异步处理大批量采集。
Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐