1. 程式人生 > >利用web端介面實現QQ好友列表獲取、QQ群成員獲取列表的例項分析

利用web端介面實現QQ好友列表獲取、QQ群成員獲取列表的例項分析

當開放API成為歷史潮流不可阻擋之時,騰訊亦只能與時俱進,但騰訊為了保持江湖的壟斷地位,不會隨意公開像QQ號這樣的客戶資源,於是乎,你能通過webQQ查詢到的,也只能是使用者或群的暱稱,絕非QQ號碼或群號。
抓包發現:webQQ每次成功登陸後,接下來一般會去實現""和"。而斷線重連的時候,通常不需要。你每次登入webQQ,騰訊伺服器便會自動分配給你和你的QQ好友一個臨時的uin;分配給群的,便是gid和gcode。
所謂“好友列表”便是好友的uin和暱稱對應起來的資訊列表。而所謂的“群列表”,則是臨時的群名稱和群gid、群gcode的對應列表。
1.獲取QQ好友資訊表:
POSThttp://s.web2.qq.com/api/get_user_friends2HTTP/1.1
Referer:http://s.web2.qq.com/proxy.html
POST資料:一個json資料結構:
r={"h":"hello","vfwebqq":"【vfwebqq】"}
如果成功,會返回一個json資料結構:
{"retcode":0,"result":{"friends":[{"flag":0,"uin":3112962973,"categories":0}],"marknames":[],"categories":[{"index":1,"sort":1,"name":"朋友"},{"index":2,"sort":2,"name":"家人"},{"index":3,"sort":3,"name":"同學"}],"vipinfo":[{"vip_level":0,"u":3112962973,"is_vip":0}],"info":[{"face":0,"flag":524288,"nick":"Spark.Ho","uin":3112962973}]}}
我們把返回的資料,存放在一個指定的txt檔案中,供其後操作的呼叫。
C++ (with libcurl)源程式
string WebQQ_buddy()
{
 // 提取QQ登入資訊:
 char buf[1024];
 string vfwebqq;
 size_t found,found2;
 ifstream QQLoginfile("D:\\SparkHo\\QQLogin.txt"); 
 if(!QQLoginfile)
 {
  QQLoginfile.close();
  vfwebqq = "";
 }
 else
 {
  QQLoginfile.getline(buf,1024);
  string QQLogin = (string) buf;
  QQLoginfile.close();
  found  = QQLogin.find("vfwebqq");
  found2 = QQLogin.find(";",found+8,1);
  vfwebqq = QQLogin.substr(found+8,found2-found-8);
 }
 // 初始化libcurl
 CURLcode return_code;
 return_code = curl_global_init(CURL_GLOBAL_WIN32);
 if (CURLE_OK != return_code) return NULL;
 // 獲取easy handle
 CURL *easy_handle = curl_easy_init();
 if (NULL == easy_handle)
 {  
  curl_global_cleanup();
  return “”;
 }
 string buffer;
 string post_url = "http://s.web2.qq.com/api/get_user_friends2";
 string referer_url = "http://s.web2.qq.com/proxy.html";
 string base = "{\"h\":\"hello\",\"vfwebqq\":\""+vfwebqq+"\"}";
 string urlencode = curl_easy_escape(easy_handle,base.c_str(),0);
 string fields = "r=" + urlencode;
  // 設定easy handle屬性
 curl_easy_setopt(easy_handle, CURLOPT_URL, post_url.c_str());
 curl_easy_setopt(easy_handle, CURLOPT_REFERER, referer_url.c_str());
 curl_easy_setopt(easy_handle, CURLOPT_POST, 1);
 curl_easy_setopt(easy_handle, CURLOPT_POSTFIELDS, fields.c_str());
 curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, writer);
 curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, &buffer);
 //提交第一步儲存的cookie 
    curl_easy_setopt(easy_handle, CURLOPT_COOKIEFILE,"D:\\SparkHo\\cookie_login.txt");
 // 執行資料請求
 curl_easy_perform(easy_handle); 
 // 釋放資源
 curl_easy_cleanup(easy_handle);
 curl_global_cleanup();
 // 更新QQ好友列表資訊:
 fstream QQBuddywrite("D:\\SparkHo\\QQBuddy.txt",ios::out|ios::trunc);
 QQBuddywrite<<buffer.c_str();
 QQBuddywrite.close();

 return buffer;
}

2.獲取群資訊表:

POSThttp://s.web2.qq.com/api/get_group_name_list_mask2HTTP/1.1
Referer:http://s.web2.qq.com/proxy.html
POST資料:一個json資料結構:
r={"vfwebqq":"【vfwebqq】"}
如果成功,會返回一個json資料結構:
{"retcode":0,"result":{"gmasklist":[],"gnamelist":[{"flag":17825793,"name":"Spark.Ho操盤","gid":2444491359,"code":2485575464}],"gmarklist":[]}}
我們把返回的資料,存放在一個指定的txt檔案中,供其後操作的呼叫。
C++ (with libcurl)源程式
string WebQQ_group()
{
 // 提取QQ登入資訊:
 char buf[1024];
 string vfwebqq;
 size_t found,found2;
 ifstream QQLoginfile("D:\\SparkHo\\QQLogin.txt"); 
 if(!QQLoginfile)
 {
  QQLoginfile.close();
  vfwebqq = "";
 }
 else
 {
  QQLoginfile.getline(buf,1024);
  string QQLogin = (string) buf;
  QQLoginfile.close();
  found  = QQLogin.find("vfwebqq");
  found2 = QQLogin.find(";",found+8,1);
  vfwebqq = QQLogin.substr(found+8,found2-found-8);
 }
 // 初始化libcurl
 CURLcode return_code;
 return_code = curl_global_init(CURL_GLOBAL_WIN32);
 if (CURLE_OK != return_code) return NULL;
 // 獲取easy handle
 CURL *easy_handle = curl_easy_init();
 if (NULL == easy_handle)
 {  
  curl_global_cleanup();
  return “”;
 }
 string buffer;
 string post_url = "http://s.web2.qq.com/api/get_group_name_list_mask2";
 string referer_url = "http://s.web2.qq.com/proxy.html";
 string base = "{\"vfwebqq\":\""+vfwebqq+"\"}";
 string urlencode = curl_easy_escape(easy_handle,base.c_str(),0);
 string fields = "r=" + urlencode;
 // 設定easy handle屬性
 curl_easy_setopt(easy_handle, CURLOPT_URL, post_url.c_str());
 curl_easy_setopt(easy_handle, CURLOPT_REFERER, referer_url.c_str());
 curl_easy_setopt(easy_handle, CURLOPT_POST, 1);
 curl_easy_setopt(easy_handle, CURLOPT_POSTFIELDS, fields.c_str());
 curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, writer);
 curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, &buffer);
 //提交第一步儲存的cookie 
    curl_easy_setopt(easy_handle, CURLOPT_COOKIEFILE,"D:\\SparkHo\\cookie_login.txt");
 // 執行資料請求
 curl_easy_perform(easy_handle); 
 // 釋放資源
 curl_easy_cleanup(easy_handle);
 curl_global_cleanup();
 // 更新QQ群列表資訊:
 fstream QQGroupwrite("D:\\SparkHo\\QQGroup.txt",ios::out|ios::trunc);
 QQGroupwrite<<buffer.c_str();
 QQGroupwrite.close();
     
 return buffer;
}
3.獲取群成員資訊表:
GET http://s.web2.qq.com/api/get_group_info_ext2?gcode=【gcode】&vfwebqq=【vfwebqq】 HTTP/1.1
Referer:http://s.web2.qq.com/proxy.html
POST資料:一個json資料結構:
r={"vfwebqq":"【vfwebqq】"}
如果成功,會返回一個json資料結構:
{"retcode":0,"result":{"stats":[{"client_type":1,"uin":1664604219,"stat":30},。。。,{"client_type":41,"uin":2393982134,"stat":10}],
"minfo":[{"nick":"David","province":"","gender":"male","uin":2010255454,"country":"","city":""}, 。。。,{"nick":"﹏落敗的唯美丶","province":"山東","gender":"female","uin":3386757496,"country":"中國","city":"青島"}],
"ginfo":{"face":0,"memo":"飛狐下載\nhttp://dl.dbank.com/c0pmgrt8wa\n\r\n通達信":"順勢而為","code":2485575464,"createtime":1260641721,"flag":17825793,"level":0,"name":"Spark.Ho操盤","gid":2444491359,"owner":3112962973,
"members":[{"muin":2010255454,"mflag":132},,,,,{"muin":3386757496,"mflag":0}],"option":2},
"cards":[{"muin":3112962973,"card":"Spark.Ho"},。。。,{"muin":2393982134,"card":"SP預警"}],
"vipinfo":[{"vip_level":0,"u":4157281859,"is_vip":0},。。。,{"vip_level":0,"u":3489781961,"is_vip":0}]}}
目前寫了完整的軟體。有想了解可加QQ好友列表獲取:578421920   QQ群:604132650