无需授权无需登录,根据微博ID获取微博昵称,10行代码

这种需求虽然比较奇葩,但可能还是会出现的,笔者就遇到过,详情就不废话了 {:soso_e113:}

笔者的微博:
[url=http://weibo.com/1864997710]http://weibo.com/1864997710[/url]
[url=http://weibo.com/xieshiliang]http://weibo.com/xieshiliang[/url]

[font=微软雅黑][size=4][b][color=Navy]直接 curl 我的微博:[/b][/size][/font]

[color=White]shiliang@localhost:/var/www$ curl 'http://weibo.com/1864997710' -v
* About to connect() to weibo.com port 80 (#0)
*   Trying 123.125.104.197... connected
> GET /1864997710 HTTP/1.1
> User-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: weibo.com
> Accept: */*
> 
< HTTP/1.1 302 Moved Temporarily
< Server: WeiBo
< Date: Thu, 18 Sep 2014 02:02:22 GMT
< Content-Type: text/html; charset=utf-8
< Transfer-Encoding: chunked
< Connection: close
< Expires: Sat, 26 Jul 1997 05:00:00 GMT
< P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"
< Cache-Control: no-cache, no-store
< Pragma: no-cache
< Location: http://passport.weibo.com/visitor/visitor?a=enter&url=http%3A%2F%2Fweibo.com%2F1864997710&_rand=1411005742.125
< DPOOL_HEADER: alice97
< Set-Cookie: TC-Page-G0=4e714161a27175839f5a8e7411c8b98c;Path=/
< LB_HEADER: alice27
< 
* Closing connection #0&#91;/code&#93;看来直接抓取网页的形式不靠谱,因为未登录,微博给你重定向了。

下面这个函数,可以帮你实现,无需授权无需登录,根据微博ID获取微博昵称
(其实就是未登录访问某人的微博时产生的邀请注册页,页面中会有TA的微博昵称,于是就呵呵了 )&#91;code&#93;function get_weibo_name($weibo_id) {
    $url = 'http://weibo.com/signup/signup.php?inviteCode=%u&lang=zh-cn';
    $url = sprintf($url, $weibo_id);
    $txt = file_get_contents($url);
    preg_match('/<div class="slogan_cont\sW_Yahei">(.*?)<\/div>/is', $txt, $match);
    if (isset($match[1]) && !empty($match[1])) {
        $find = array(' ', 'Hi,我是', '!赶快注册微博粉我吧,随时分享我的最新动态!');
        $name = str_replace($find, '', trim($match[1]));
        return $name ?: false;
    }
    return false;
}

最新文章

Return Top