简单记录一下代码片段,没什么特别内容。参考微信接口API
int CWeChatHttp::get_new_openids_from_old_openids(const std::string& old_app_id, const std::string& access_token, std::list<std::string>& old_openid_lst)
{
int iSucCount = 0;
if (old_openid_lst.size() > 100)
{
return -1;
}
Document doc;
doc.SetObject();
Document::AllocatorType& allocate = doc.GetAllocator();
Value from_appid(StringRef(old_app_id.c_str()));
doc.AddMember("from_appid", from_appid, allocate);
rapidjson::Value openid_list(rapidjson::kArrayType);
std::list<std::string>::iterator it = old_openid_lst.begin();
for (; it != old_openid_lst.end(); ++it)
openid_list.PushBack(StringRef((*it).c_str()), allocate);
doc.AddMember("openid_list", openid_list, allocate);
StringBuffer buffer;
rapidjson::Writer<StringBuffer> write(buffer);
doc.Accept(write);
std::string strPostJson = buffer.GetString();
CURL* curl = NULL;
CURLcode res = CURL_LAST;
char szReqUrl[1024] = { 0 };
std::string strResponse = "";
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (NULL != curl)
{
sprintf_s(szReqUrl, "https://api.weixin.qq.com/cgi-bin/changeopenid?access_token=%s", access_token.c_str());
struct curl_slist* pHeaders = NULL;
pHeaders = curl_slist_append(pHeaders, "Accept: application/json");
pHeaders = curl_slist_append(pHeaders, "Content-Type:application/json;charset=UTF-8");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, pHeaders);
curl_easy_setopt(curl, CURLOPT_URL, szReqUrl);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPostJson.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CWeChatHttp::s_write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&strResponse);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
res = curl_easy_perform(curl);
if (CURLE_OK == res)
{
iSucCount = save_response(strResponse);
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return iSucCount;
}
int CWeChatHttp::save_response(const std::string& response)
{
int iRet = 0;
Document docResponse;
if (docResponse.Parse<0>(response.c_str()).HasParseError())
{
printf("[%s]rapidjson Prase failed: %d\n", __FUNCTION__, docResponse.GetParseError());
return iRet;
}
if (docResponse.HasMember("errcode") && 0 != docResponse["errcode"].GetInt())
{
printf("[%s]response errcode: %d,errmsg: %s\n", __FUNCTION__, docResponse["errcode"].GetInt(), docResponse["errmsg"].GetString());
return iRet;
}
if (!docResponse.HasMember("result_list"))
{
printf("[%s]response not found result_list.\n", __FUNCTION__);
return iRet;
}
const Value& listArray = docResponse["result_list"];
if (listArray.IsArray())
{
FILE* fp = fopen(GetSaveOpenIdFile(save_conver_openid_file).c_str(), "ab+");
if (NULL != fp)
{
for (SizeType i = 0; i < listArray.Size(); ++i)
{
const Value& value = listArray[i];
if (value.IsObject())
{
if (value.HasMember("err_msg") && value.HasMember("ori_openid"))
{
if (0 != stricmp(value["err_msg"].GetString(), "ok"))
{
printf("[%s]ori_openid: %s,err_msg: %s\n", __FUNCTION__, value["ori_openid"].GetString(), value["err_msg"].GetString());
}
else
{
if (value.HasMember("new_openid"))
{
printf("conver flag:%d\n", m_iFlag);
fprintf(fp, "%s,%s\n", value["ori_openid"].GetString(), value["new_openid"].GetString());
m_iFlag++;
iRet++;
}
}
}
}
}
fclose(fp);
fp = NULL;
}
else
{
printf("[%s]fopen file[%s] failed: %d\n", __FUNCTION__, save_conver_openid_file.c_str(), GetLastError());
}
}
return iRet;
}