在这里插入图片描述

批量请求指定的接口以检查多个微信小程序是否被封禁。接口返回的 JSON 数据可以告诉我们小程序是否被封禁。若返回的 code 为 0,则表示该小程序已被封禁;若为 1,则表示正常。
C++ 使用 libcurl 库来发送 HTTP 请求,并使用 nlohmann/json 库解析返回的 JSON 数据。您可以在 C++ 中利用这两个库实现批量检查微信小程序是否被封禁的功能。
要求:

1.安装 libcurl 库:用于发送 HTTP 请求。
2.安装 nlohmann/json 库:用于解析返回的 JSON 数据。

C++ 批量请求源码:
#include <iostream>
#include <string>
#include <curl/curl.h>
#include <nlohmann/json.hpp>
#include <vector>

using json = nlohmann::json;

// 定义回调函数,用于处理返回的 HTTP 响应数据
size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}

// 执行 HTTP GET 请求并返回响应内容
std::string performRequest(const std::string& url) {
CURL* curl;
CURLcode res;
std::string response;

curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();

if (curl) {
    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &amp;response);
    res = curl_easy_perform(curl);

    if (res != CURLE_OK) {
        std::cerr &lt;&lt; "curl_easy_perform() failed: " &lt;&lt; curl_easy_strerror(res) &lt;&lt; std::endl;
    }

    curl_easy_cleanup(curl);
}

curl_global_cleanup();
return response;

}

// 批量检查微信小程序是否被封禁
void checkAppStatus(const std::vector<std::string>& appIds) {
std::string baseUrl = “https://down.ychengsnsm.com/xcx/checkxcx.php?appid=”;

for (const auto&amp; appId : appIds) {
    std::string url = baseUrl + appId;
    std::string response = performRequest(url);

    // 解析 JSON 数据
    try {
        json resJson = json::parse(response);
        int code = resJson["code"];
        std::string status = resJson["status"];

        std::cout &lt;&lt; "App ID: " &lt;&lt; appId &lt;&lt; std::endl;
        std::cout &lt;&lt; "Code: " &lt;&lt; code &lt;&lt; std::endl;
        std::cout &lt;&lt; "Status: " &lt;&lt; status &lt;&lt; std::endl;
        std::cout &lt;&lt; "-----------------------------" &lt;&lt; std::endl;

    } catch (const json::exception&amp; e) {
        std::cerr &lt;&lt; "JSON parsing error: " &lt;&lt; e.what() &lt;&lt; std::endl;
    }
}

}

int main() {
// 示例小程序的 AppID 列表
std::vector<std::string> appIds = {
“wx81894c6dbb81c2e2”,
“wx1234567890abcdef”,
“wx0987654321fedcba”
};

// 批量请求并检查状态
checkAppStatus(appIds);

return 0;

}

代码解析:

3.performRequest 函数:

4.使用 libcurl 库发送 HTTP GET 请求并返回响应内容。
5.WriteCallback 是一个回调函数,用于获取返回的数据。

6.checkAppStatus 函数:

7.遍历传入的小程序 appid 列表,逐个发送请求。
8.解析返回的 JSON 数据并提取 code 和 status 字段。

9.main 函数:

10.初始化一个示例的小程序 appid 列表。
11.调用 checkAppStatus 函数进行批量检查。

依赖库说明:

12.libcurl: 用于发送 HTTP 请求。
13.nlohmann/json: 用于解析 JSON 格式的数据。

安装步骤:

14.安装 libcurl:

15.Linux 上:sudo apt-get install libcurl4-openssl-dev
16.macOS 上:brew install curl

17.安装 nlohmann/json:

18.可以使用 vcpkg 或 conan 安装,或者直接将 json.hpp 文件包含到项目中。

编译和运行:

19.使用 g++ 编译:

g++ -std=c++11 -o check_app_status check_app_status.cpp -lcurl
./check_app_status

此代码将输出每个小程序的 code 和 status,帮助您判断它们是否被封禁。

Logo

助力广东及东莞地区开发者,代码托管、在线学习与竞赛、技术交流与分享、资源共享、职业发展,成为松山湖开发者首选的工作与学习平台

更多推荐