PHP模拟评论生成微博t.cn短网址

3 min read

其实早在 2018年 就写过 PHP调用新浪API生成短网址,但是 2020年 的时候微博发布了 微博平台实施外链白名单机制的公告,限制了只有 白名单内域名 才可以正常生成跳转, 非白名单域名 跳转会出现提示信息,并点击 继续访问 按钮后才可继续跳转。

最近有人找我写 t.cn 短链接的生成,对方仅用作缩短 QQ群加群链接 ,所以不存在白名单的问题,因为 qq.com 肯定存在于白名单内。

我们直接通过发表评论的方式生成微博短网址,然后自动删除刚才发布的评论。

获取MID

简单说一下各版本微博获取微博 MID 的方法

手机版

https://m.weibo.cn/{用户UID}/{微博MID}

电脑版

https://weibo.com/{用户UID}/{微博字符串}

访问微博 API 接口获取 MID

https://weibo.com/ajax/statuses/show?id={微博字符串}

返回的 json 数据中 id / idstr / mid 均为微博 MID

国际版

https://share.api.weibo.cn/share/{用户UID},{微博MID}.html?weibo_id={微博MID}

源代码

填写你的微博 mid 和微博 Cookie 中的 SUB 字段(全部填写也行)即可。

<?php
/*
title: 微博短网址生成
description: 通过评论的方式生成微博短网址,并自动删除发布的评论
author: iami233
date: 2022-06-12
*/
error_reporting(0);
header('Access-Control-Allow-Origin:*');
header('Content-type: application/json');

$url = $_GET['url'];
$mid = ''; //需要评论的微博mid,纯数字

if (empty($url)) {
    $Json = array(
        'code' => '201',
        'msg' => '生成失败',
    );
    $Json = json_encode($Json,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);
    echo stripslashes($Json);
    return $Json; 
}else{
    postComment($url,$mid);
}

function postComment($url, $mid) {
    $url = "https://www.weibo.com/aj/v6/comment/add";
    $data = array(
        'mid' => $mid,
        'content' => $url
    );
    $arr = json_decode(curl($url, $data), true);

    if ($arr['code'] == '100000') {
        $data = $arr['data']['comment'];
        preg_match('/title=\"网页链接\" href=\"(.*?)\"/', $data, $shortUrl);
        preg_match('/comment_id=\"(.+\d)\"/', $data, $commentId);
        $Json = array(
            'code' => '200',
            'msg' => '生成成功',
            'data' => array(
                'comment_id' => $commentId[1],
                'short_url' => $shortUrl[1]
            )
        );
        delCommen($commentId[1], $mid);           
    } else {
        $Json = array(
            'code' => '201',
            'msg' => '生成失败',
        );
    }
    $Json = json_encode($Json,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);
    echo stripslashes($Json);
    return $Json; 
}

function delCommen($cid, $mid) {
    $url = "https://www.weibo.com/aj/comment/del";
    $data = array(
        'mid' => $mid,
        'cid' => $cid
    );
    $arr = curl($url, $data);
}

function curl ($url, $post = null) {
    $headers[] = 'Cookie: SUB=你的cookie';
    $headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36';
    $headers[] = 'Content-Type : application/x-www-form-urlencoded';
    $headers[] = 'Referer: https://www.weibo.com';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
    if (!empty($post)) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    }
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}