前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >通过微博的mid获取微博的URL

通过微博的mid获取微博的URL

作者头像
老高的技术博客
发布2022-12-24 12:27:46
6860
发布2022-12-24 12:27:46
举报

代码网上参考的,一共有两种,自己优化了一下:

1.离线方法

参考:http://www.iganlei.cn/demo/186.html

代码语言:javascript
复制
<?php
    function int10to62($int10)
    {
        static $str62keys;
        $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");
        $s62       = '';
        $r         = 0;
        while ($int10 != 0) {
            $r     = $int10 % 62;
            $s62   = $str62keys[$r] . $s62;
            $int10 = floor($int10 / 62);
        }
        return $s62;
    }
    
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);
        $num     = int10to62($num);
        $url     = $num . $url;
    }
    return $url;
}

function getNewUrl($uid,$mid)
{
    $newUrl = 'http://weibo.com/' . $uid . '/' . getCodeByMid($mid);
    return $newUrl;
}


echo getNewUrl('phpgao', '3524952365496186');

2.api方法

参考:http://blog.csdn.net/k1988/article/details/6684114

mid说明:http://open.weibo.com/wiki/Querymid

代码语言:javascript
复制
$re = json_decode(file_get_contents("http://api.t.sina.com.cn/queryid.json?mid=xhMRc8nNu&isBase62=1&type=1"));
$id = $re->id;
echo $id;
 
$re = json_decode(file_get_contents("http://api.t.sina.com.cn/querymid.json?id=$id"));
echo $re->mid;
exit;
本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-05-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com