今天除夕,祝大家新年快乐,这里分享一个 URL.CN 短网址接口原理

in Share with 0 comment

新年了

今天除夕,马上春节了,祝大家新年快乐。在这里分享一个 url.cn 短网址生成原理。

url.cn 接口

url.cn 是腾讯旗下的,这里分享 新的接口生成短网址

接口地址:https://openmobile.qq.com/api/url_change
请求方式:post
请求Headers :

Referer=http://openmobile.qq.com/
User-Agent=AndroidSDK

请求主体参数:

value={"url":"https://www.alapi.cn"} 
# value 值是 json 格式的,上面的 https://www.alapi.cn 替换成你要缩短的网址就可以了。

最后请求还需要带上两个 cookie
分别是 uinskey 的值。这个值,你只要使用qq登陆了QQ空间等腾讯旗下的网站就可以在cookie 里面查到的

正确返回格式

{
    "retcode": 0,
    "msg": "ok",
    "result": {
        "list": [
            "https://url.cn/58UOt4U"
        ]
    }
}

实现代码

PHP

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://openmobile.qq.com/api/url_change",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "value=%7B%22url%22%3A%22https%3A%2F%2Fwww.alapi.cn%22%7D&undefined=",
  CURLOPT_HTTPHEADER => array(
    "Content-Type: application/x-www-form-urlencoded",
    "Cookie: uni=你的uni值;skey=你的skey值",
    "Referer: http://openmobile.qq.com/",
    "User-Agent: AndroidSDK",
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

java

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "value=%7B%22url%22%3A%22https%3A%2F%2Fwww.alapi.cn%22%7D&undefined=");
Request request = new Request.Builder()
  .url("https://openmobile.qq.com/api/url_change")
  .post(body)
  .addHeader("Referer", "http://openmobile.qq.com/")
  .addHeader("Content-Type", "application/x-www-form-urlencoded")
  .addHeader("User-Agent", "AndroidSDK")
  .addHeader("Cookie", "uni=你的uni值;skey=你的skey值")
  .addHeader("cache-control", "no-cache")
  .build();

Response response = client.newCall(request).execute();

python

import http.client

conn = http.client.HTTPConnection("openmobile,qq,com")

payload = "value=%7B%22url%22%3A%22https%3A%2F%2Fwww.alapi.cn%22%7D&undefined="

headers = {
    'Referer': "http://openmobile.qq.com/",
    'Content-Type': "application/x-www-form-urlencoded",
    'User-Agent': "AndroidSDK",
    'Cookie': "uni=你的uni值;skey=你的skey值",
    'cache-control': "no-cache",
    }

conn.request("POST", "api,url_change", payload, headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))
Responses