Skip to content

下载图片 ​

根据图片消息回调中的 XML 数据下载图片,并返回可访问的图片地址。

接口地址: POST /getMsgImg

前置条件 ​

  • 当前 wId 对应的微信实例在线。
  • msgId、content 来自消息回调,不建议手动拼接。
  • 部分图片可能只有高清图、常规图或缩略图中的一种,下载失败时可切换 type 重试。

请求参数 ​

参数名必选类型说明
wId是string登录实例标识
msgId是number消息 ID
content是string图片消息回调中的 XML 数据
type否int图片类型:0 常规图片,1 高清图

请求示例 ​

http
POST {BASE_URL}/getMsgImg
Authorization: Bearer <你的调用凭证>
Content-Type: application/json

{
  "wId": "{{wId}}",
  "msgId": 1102684156,
  "content": "<?xml version=\"1.0\"?><msg><img aeskey=\"07fea09b27952d512c0d71a52c914f3\" cdnthumburl=\"305802010004...\" cdnmidimgurl=\"305802010004...\" length=\"51361\" md5=\"449fb858f24416adcab831859011fb21\" /></msg>",
  "type": 0
}
bash
curl -X POST '{BASE_URL}/getMsgImg' \
  -H 'Authorization: Bearer <你的调用凭证>' \
  -H 'Content-Type: application/json' \
  --data-raw '{
  "wId": "{{wId}}",
  "msgId": 1102684156,
  "content": "<?xml version=\"1.0\"?><msg><img aeskey=\"07fea09b27952d512c0d71a52c914f3\" cdnthumburl=\"305802010004...\" cdnmidimgurl=\"305802010004...\" length=\"51361\" md5=\"449fb858f24416adcab831859011fb21\" /></msg>",
  "type": 0
}'
python
import requests

url = "{BASE_URL}/getMsgImg"
headers = {"Authorization": "Bearer <你的调用凭证>", "Content-Type": "application/json"}
body = r"""{
  "wId": "{{wId}}",
  "msgId": 1102684156,
  "content": "<?xml version=\"1.0\"?><msg><img aeskey=\"07fea09b27952d512c0d71a52c914f3\" cdnthumburl=\"305802010004...\" cdnmidimgurl=\"305802010004...\" length=\"51361\" md5=\"449fb858f24416adcab831859011fb21\" /></msg>",
  "type": 0
}"""

resp = requests.post(url, headers=headers, data=body.encode("utf-8"))
print(resp.text)
java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

String url = "{BASE_URL}/getMsgImg";
String body = "{\n" +
    "  \"wId\": \"{{wId}}\",\n" +
    "  \"msgId\": 1102684156,\n" +
    "  \"content\": \"<?xml version=\\\"1.0\\\"?><msg><img aeskey=\\\"07fea09b27952d512c0d71a52c914f3\\\" cdnthumburl=\\\"305802010004...\\\" cdnmidimgurl=\\\"305802010004...\\\" length=\\\"51361\\\" md5=\\\"449fb858f24416adcab831859011fb21\\\" /></msg>\",\n" +
    "  \"type\": 0\n" +
    "}";

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
    .header("Authorization", "Bearer <你的调用凭证>")
    .header("Content-Type", "application/json")
    .POST(HttpRequest.BodyPublishers.ofString(body))
    .build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
php
<?php

$url = '{BASE_URL}/getMsgImg';
$body = <<<'JSON'
{
  "wId": "{{wId}}",
  "msgId": 1102684156,
  "content": "<?xml version=\"1.0\"?><msg><img aeskey=\"07fea09b27952d512c0d71a52c914f3\" cdnthumburl=\"305802010004...\" cdnmidimgurl=\"305802010004...\" length=\"51361\" md5=\"449fb858f24416adcab831859011fb21\" /></msg>",
  "type": 0
}
JSON;

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer <你的调用凭证>',
    'Content-Type: application/json',
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

响应参数 ​

参数名类型说明
codestring1000 成功;其他值都是失败,通常是 1001
messagestring反馈信息
dataobject下载结果
data.urlstring图片地址,通常 7 日内有效

成功响应 ​

json
{
  "code": "1000",
  "message": "成功",
  "data": {
    "url": "https://cdn.e-yun.example/msgImg/dd32565c-78b0-4803-a330-6293b05674d9.png"
  }
}

错误响应 ​

json
{
  "code": "1001",
  "message": "失败",
  "data": null
}

使用建议 ​

  • 优先使用消息回调原始 XML,不要只复制其中部分字段。
  • 下载失败时先切换 type=0/1 重试,再排查 wId 是否在线。
  • 返回的 url 有有效期,业务侧需要及时转存或消费。