在弄新浪api玩时发现不能直接得到发布微博后的地址。调用的api返回值中有个mid值(和id值一样),
http://weibo.com/2156605207/zxsTw0O4I
先百度了下 怎样把返回的json中的mid值转成单微博地址中的字串,,,发现较多人博客文章里提供了一个php 代码如下
<?php $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" ); /** * 10进制值转换为62进制 * @param {String} int10 10进制值 * @return {String} 62进制值 */ function int10to62($int10) { global $str62keys; $s62 = ''; $r = 0; while ($int10 != 0) { $r = $int10 % 62; $s62 = $str62keys[$r].$s62; $int10 = floor($int10 / 62); } return $s62; } /** * * 通过mid获得短格式 * @param string $mid * @return 短格式 */ function getCodeByMid($mid){ $url = ''; for ($i = strlen($mid) - 7; $i > -7; $i -=7)//从最后往前以7字节为一组读取mid { $offset1 = $i < 0 ? 0 : $i; $offset2 = $i + 7; $num = substr($mid, $offset1,$offset2-$offset1); //mid.substring(offset1, offset2); $num = int10to62($num); $url = $num .$url; } return $url; } echo getCodeByMid('221110410216147026'); ?>根据自己发的微博的数字mid :3579758980192492 用上述方法得到的 url mid 为 zxsTwO4I , 而实际的url为:zxsTw0O4I。。
后来发现 新浪提供了个api :[http://open.weibo.com/wiki/Querymid](http://open.weibo.com/wiki/Querymid)
{ "mid": "8Ras3qlz" }
$mid = json_decode(file_get_contents(“http://api.t.sina.com.cn/querymid.json?id=".$result['mid‘]));
echo $mid[‘mid’];
输出结果与单条微博地址中的URL一致。。
新浪还提供了反查api:[http://open.weibo.com/wiki/Queryid
](http://open.weibo.com/wiki/Queryid)新浪微博地址url字符与mid的相互转换截图如下
简单demo: [![QQ截图20130519203414](http://www.iganlei.cn/wp-content/uploads/2013/05/QQ截图20130519203414-300x192.png)](http://www.iganlei.cn/wp-content/uploads/2013/05/QQ截图20130519203414.png) 发布微博并获取地址: [![QQ截图20130519203530](http://www.iganlei.cn/wp-content/uploads/2013/05/QQ截图20130519203530-300x157.png)](http://www.iganlei.cn/wp-content/uploads/2013/05/QQ截图20130519203530.png)