新浪微博地址url字符与mid的相互转换算法及原理

通过新浪微博api接口发布微博成功后,会返回一个二维数组,这个返回的数组里有微博的内容,发布微博的用户信息等等.却没有所发布成功的微博的URL.很多时候如果要记录信息在数据库 则微博URL应该必不可少.通过新浪微博论坛搜索,发现有以下方法.


原理:

新浪微博的URL都是如:http://weibo.com/1247667622/l4EV85aqp 这样三部分.


第一部分(绿色部分)为新浪微博的域名,第二部分(红色部分)为博主Uid,第三部分(蓝色)为一串貌似随机的字符串.


如果通过方法能计算出蓝色字串与返回的数组里的对应关系则好解决多了.


首先分组蓝色字串 ,从后往前4个字符一组,得到以下三组字符:
l
4EV8
5aqp


将它们分别转换成62进制的数值则为 21,111061, 1231717  将它们组合起来就是一串 2111106141231717 类似这样的字串


通过发现返回的二维数组里有个 [mid]的字段,其返回结果为 2111106141231717.刚好是对应的.


故:通过api接口发布微博,接收其成功后返回的数组,提取其里面的 mid  将mid转化成 62进字字串,再将发布微博的UID组合.


原码

附1:写了个mid与url相互转换的Javascript类(来自网络)
附2:php版 字串转id函数
 
 function sinaWburl2ID($url) {
	$surl[2] = str62to10(substr($url, strlen($url) - 4, 4));
	$surl[1] = str62to10(substr($url, strlen($url) - 8, 4));
	$surl[0] = str62to10(substr($url, 0, strlen($url) - 8));
	$int10 = $surl[0] . $surl[1] . $surl[2];
	return ltrim($int10, '0');
}
function str62to10($str62) { //62进制到10进制
	$strarry = str_split($str62);
	$str = 0;
	for ($i = 0; $i < strlen($str62); $i++) {
		$vi = Pow(62, (strlen($str62) - $i -1));
 
		$str += $vi * str62keys($strarry[$i]);
	}
	$str = str_pad($str, 7, "0", STR_PAD_LEFT);
	return $str;
}
 
function str62keys($ks) //62进制字典
{
	$str62keys = array (
		"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q",
		"r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q",
		"R","S","T","U","V","W","X","Y","Z"
	);
	return array_search($ks, $str62keys);
}
echo  sinaWburl2ID('l4EV85aqp '); //调用



php版Mid转Str版

function midToStr($mid) {
	settype($mid, 'string');
	$mid_length = strlen($mid);
	$url = '';
	$str = strrev($mid);
	$str = str_split($str, 7);
 
	foreach ($str as $v) {
		$char = intTo62(strrev($v));
		$char = str_pad($char, 4, "0");
		$url .= $char;
	}
 
	$url_str = strrev($url);
 
	return ltrim($url_str, '0');
}
 
function str62keys_int_62($key) //62进制字典
{
	$str62keys = array (
		"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q",
		"r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q",
		"R","S","T","U","V","W","X","Y","Z"
	);
	return $str62keys[$key];
}
 
/* url 10 进制 转62进制*/
 
function intTo62($int10) {
	$s62 = '';
	$r = 0;
	while ($int10 != 0) {
		$r = $int10 % 62;
		$s62 .= str62keys_int_62($r);
		$int10 = floor($int10 / 62);
	}
 
	return $s62;
}
echo midToStr($mid);//调用

新浪API方法

$char_id='y6D72AsN7';
//通过url 字符id获取数字mid
$re = json_decode(file_get_contents("http://api.t.sina.com.cn/queryid.json?mid=$char_id&isBase62=1&type=1"));
echo $id = $re->id;
 
//通过数字mid获取url 字符id
$re = json_decode(file_get_contents("http://api.t.sina.com.cn/querymid.json?id=$id"));
echo   $re->mid;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值