jQuery Online Generator for t.cn Sina Short URLs

6,646 0

I’ve recently been learning Ajax, so I often write some small demos, such as the previous “jQuery Online Retrieval of QQ Names and Avatars”. Today’s project is also an Ajax project. Its main function is to generate Sina t.cn short URLs online. I’ve seen many URL shorteners online, and they all require entering a URL and then clicking a button before they execute. Mine doesn’t require that.

HTML Code

HTML
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>在线生成 t.cn 短网址</title>
        <script src="https://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
    </head>
    <body>
        <div> 长链接 <input type="text" name="" id="longurl" value="" /></div><br>
        <div> 短链接 <input type="text" name="" id="shorturl" value="" /></div><br>
        <script>
            $("#longurl").blur(function() {
                var url = $("#longurl").val();
                $.ajax({
                    type: "get",
                    url: "https://v1.ikxin.com/api/tcnurl.php?from=txt&url=" + url,
                    success: function(data) {
                        $("#shorturl").val(data);
                    },
                    error: function() {
                        $("#shorturl").val("获取失败");
                    }
                });
            })
        </script>
    </body>
</html>

PHP Code

PHP
<?php
$apiurl = 'http://api.t.sina.com.cn/short_url/shorten.json';  // 新浪短网址 json 接口
$source = '3271710248';                                       // 新浪 source 验证码 
$geturl = $_GET ['url'];
$format = $_GET ['format'];

switch ($format){
    case "text":
        if(substr($geturl, 0, 4) != 'http'){
            echo "您的输入有误!";
        }else{
            $suo_url = sprintf($apiurl.'?source='.$source.'&url_long='.$geturl);
            $dwzcode = file_get_contents($suo_url);
            $dwz = str_replace(array('[',']'), '', $dwzcode);
            $dwzurl = json_decode($dwz,true);
            echo $dwzurl['url_short'];
        }
        break;
    case "json":
        if(substr($geturl, 0, 4) != 'http'){
            echo "您的输入有误!";
        }else{
            $suo_url = sprintf($apiurl.'?source='.$source.'&url_long='.$geturl);
            $dwzcode = file_get_contents($suo_url);
            $oldarr = array('[',']','url_short','url_long',',"type":0');
            $newarr = array('','','shorturl','longurl','');
            $dwz = str_replace($oldarr, $newarr, $dwzcode);
            echo $dwz;
        }
        break;
    case "jsonp":
        if(substr($geturl, 0, 4) != 'http'){
            echo "您的输入有误!";
        }else{
            $suo_url = sprintf($apiurl.'?source='.$source.'&url_long='.$geturl);
            $dwzcode = file_get_contents($suo_url);
            $oldarr = array('[',']','url_short','url_long',',"type":0','{','}');
            $newarr = array('','','shorturl','longurl','','callbackUrl({','})');
            $dwz = str_replace($oldarr, $newarr, $dwzcode);
            echo $dwz;
        }
        break;
    default:
        echo "您的输入有误!";
}

Comments

(0)

No comments yet. Start the conversation.

Leave a comment

© 2026 Carefree. All rights reserved.

Carefree is a personal site about technology and everyday life, documenting web development, backend, and DevOps practices, plus software tools, digital experiences, and thoughts beyond code.