读取指定用户微博及评论-Two Undocumented Wei

2018-11-23  本文已影响0人  坚果jimbowhy

随笔安利两个有用的undocumented Weibo API

/**
 * [移动端非公开容器接口] 查询用户信息
 * 可以获取指定用户 continnerid 用于查询用户的贴子,可解决 statuses/show.json 升级后无法获取指定用户微博问题。
 * 微博网站有【首页/微博/相册】三个主要的功能标签,都有一个特定的containerid关联,有些用户有额外的功能,如超话。
 * curl -d "jumpfrom=weibocom&type=uid&value=1594052081&containerid=2314751594052081" -X POST https://m.weibo.cn/api/container/getIndex
 * 
 * 示例用户uid 主页containerid   微博containerid  相册containerid  超话containerid
 * 1891442924 2302831891442924 1076031891442924 1078031891442924
 * 1594052081 2302831594052081 1076031594052081 1078031594052081 2314751594052081
 *
 * @param  integer $uid user identifier
 * @param  integer $containerid container identifier
 * @param  integer $page page start from 1, 0 is default value.
 * @param  integer $count  unknown effect.
 * @return [type]       [description]
 */
public function weibo_getIndex($uid=1891442924,$containerid=false, $page=0, $count=5){
    $args = ([
        "jumpfrom=weibocom",
        "type=uid",
        "value=$uid",
        "page=$page",
        "count=$count",
    ]);
    if( $containerid ) $args[] = "containerid=$containerid";
    $url = "https://m.weibo.cn/api/container/getIndex?".implode("&",$args);
    $res = $this->WebRequest($url);
    if( $_REQUEST['debug'] ) print_r([$url, json_encode(json_decode($res), JSON_UNESCAPED_UNICODE)]);
    return json_decode($res);
}

/**
 * 非公开热评接口,主要数据hot_data
 * 参考  https://m.weibo.cn/api/comments/show?id=4309124919542169&page=1
 * 小白爬取单个微博用户的评论 - Denise_hzf - 博客园 
 * https://www.cnblogs.com/Denise-hzf/p/7927852.html
 * 单条微博页面地址格式:https://m.weibo.cn/status/4268203603241009
 * http://www.jinian.com/wxappApi3/weibo_comments_hotshow?id=4309279315339295
 * http://www.jinian.com/wxappApi3/weibo_comments_hotshow?id=4268203603241009
 * [weibo_comments_hotshow description]
 * @param  integer $id   [description]
 * @param  integer $page [description]
 * @return [type]        [description]
 */
public function weibo_comments_hotshow($id=4309124919542169, $page=0){
    $args = ([
        "id=$id",
        "page=$page",
    ]);
    if( $containerid ) $args[] = "containerid=$containerid";
    $url = "https://m.weibo.cn/api/comments/show?".implode("&",$args);
    $res = $this->WebRequest($url);
    echo json_encode([$url,json_decode($res)], JSON_UNESCAPED_UNICODE);
}

上面两个API使用的WebRequest方法定义如下:


    protected function WebRequest($url, $data = null, $ajax=false)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        if( $ajax ){
            $headers = ["X-Requested-With: XMLHttpRequest"]; // ajax
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }
        if (!empty($data)){
            $hasFile = false;
            if( is_array($data) ) foreach ($data as $key => $value) { // upload file for php 5.6+
                if( strpos($value,'@')!==false ){
                    $hasFile = true;
                    $data[$key] = $this->makeCurlFile(substr($value, 1));
                }
            }
            if( !$hasFile && is_array($data) ) $data = http_build_query($data);
            curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
            curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            // curl_setopt($ch, CURLOPT_POSTFIELDS, Array) upload file by @path for php 5.5 and below
        }else{
            curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
上一篇下一篇

猜你喜欢

热点阅读