PHP simulates comments to generate short URLs for Weibo t.cn

Original link: https://5ime.cn/sinaurl.html

In fact, as early as 2018年I wrote that PHP calls Sina API to generate short URLs , but 2020年Weibo released an announcement on the implementation of the external chain whitelist mechanism on the Weibo platform , restricting that only白名单内域名can be generated normally. , the非白名单域名jump will show a prompt message, and you can continue to jump after clicking the继续访问button.

Recently, someone asked me to write about the generation of t.cn short links. The other party is only used to shorten QQ群加群链接, so there is no whitelist problem, because qq.com must exist in the whitelist.

We directly generate the short URL of Weibo by posting a comment, and then automatically delete the comment just posted.

get MID

Briefly talk about the method of each version of Weibo to obtain Weibo MID

Mobile version

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

PC version

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

Access the Weibo API to get the MID

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

In the returned json data, id / idstr / mid are all microblog MID

International Edition

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

source code

Fill in the SUB fields in your Weibo mid and Weibo Cookie (all are also fine).

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
twenty one
twenty two
twenty three
twenty four
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
 <?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 ;
}

This article is reprinted from: https://5ime.cn/sinaurl.html
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment