Conversion between Weibo url (id) and mid

Source: Internet
Author: User
After Weibo is successfully published through the Sina Weibo api, a two-dimensional array is returned, which contains Weibo content and Weibo user information. but there is no URL for successfully published Weibo. in many cases, if you want to record information in the database, Weibo URL should

After Weibo is successfully published through the Sina Weibo api, a two-dimensional array is returned, which contains Weibo content and Weibo user information. but there is no URL for successfully published Weibo. in many cases, Weibo URL should be indispensable if you want to record information in the database. search through Sina Weibo Forum and find the following methods.

Principle:

Sina Weibo has three URLs: http://weibo.com/2480531040/z8elgbleq.

The first part (green part) is the Sina Weibo domain name, the second part (red part) is the blogger Uid, and the third part (blue) is a string that looks like a random string.

If the method can calculate the correspondence between the blue string and the returned array, the solution is much better.

First, group the Blue string, four characters from the back to the front, and get the following three groups of characters:
Z
8Elg
BLeQ

Convert them into 62 hexadecimal values 35,206 1702, and 8999724 combine them into a string of 3520617028999724 and similar strings.

It is found that there is a [mid] field in the returned two-dimensional array, and the returned result is 3520617028999724. it is exactly the same.

Therefore, you can use the api to publish a microblog, receive the returned array, extract the mid in it, convert the mid into a string of 62 characters, and then combine the UID of the microblog.


C # Source code

////// Obtain the id of the Weibo account from a Sina Weibo address //////One Weibo address///
 
  
Weibo id
 Public static string GetIdFromUrl (string url) {string mid = GetMidFromUrl (url); if (string. isNullOrEmpty (mid) {return string. empty;} else {return Mid2Id (mid );}}////// Obtain the Weibo mid from a Sina Weibo address //////One Weibo address///
 
  
Weibo mid
 Public static string GetMidFromUrl (string url) {if (string. IsNullOrEmpty (url) {return "" ;}if (url. IndexOf ('? ')! =-1) {url = url. Substring (0, url. IndexOf ('? ');} Regex reg = new Regex (@ "^ http: // (e \.)? Weibo \. com/[0-9a-zA-Z] + /(?
 
  
[0-9a-zA-Z] +) $ ", RegexOptions. ignoreCase); Match match = reg. match (url); if (match. success) {return match. result ("$ {id}");} return "";}///
  /// Convert Sina Weibo mid to id //////
  Weibo mid///
  
   
Return Weibo id
  Public static string Mid2Id (string mid) {string id = ""; for (int I = mid. length-4; I>-4; I = I-4) // read a set of URL characters from the last forward in 4 bytes {int offset1 = I <0? 0: I; int len = I <0? Mid. length % 4: 4; var str = mid. substring (offset1, len); str = Str62toInt (str); if (offset1> 0) // if it is not the first group, add 0 {while (str. length <7) {str = "0" + str ;}id = str + id;} return id ;}///
  /// Sina Weibo id to mid //////
  Weibo id///
  
   
Back to Weibo's mid
  Public static string Id2Mid (string id) {string mid = "", strTemp; int startIdex, len; for (var I = id. length-7; I>-7; I = I-7) // read the mid {startIdex = I <0? 0: I; len = I <0? Id. length % 7: 7; strTemp = id. substring (startIdex, len); mid = IntToStr62 (Convert. toInt32 (strTemp) + mid;} return mid;} // convert the string to the 10-digit public static string Str62toInt (string str62) {Int64 i64 = 0; for (int I = 0; I <str62.Length; I ++) {Int64 Vi = (Int64) Math. pow (62, (str62.Length-I-1); char t = str62 [I]; i64 + = Vi * GetInt10 (t. toString ();} return i64.ToString ();} // convert the value to 62 in hexadecimal notation public Static string IntToStr62 (int int10) {string s62 = ""; int r = 0; while (int10! = 0) {r = int10 % 62; s62 = Get62key (r) + s62; int10 = int10/62;} return s62 ;} // 62-digit dictionary private static string str62keys = "0123456789 bytes"; // Obtain the 62-digit integer private static Int64 GetInt10 (string key) {return str62keys corresponding to the key. indexOf (key);} // Obtain the key private static string Get62key (int int10) {if (int10 <0 | int10> 61) return ""; return str62keys. substring (int10, 1 );}
 


The APIs provided by Sina are directly input in the browser

Http://api.t.sina.com.cn/queryid.json? Mid = z8ElgBLeQ & isBase62 = 1 & type = 1 returns {"id": "3520617028999724"} http://api.t.sina.com.cn/querymid.json? Id = 3520617028999724 returns {"mid": "z8ElgBLeQ "}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.