短地址亦短网址
http://www.iganlei.cn/tool/shorturl.php
- <?php
- header(“content-type:text/html;charset=utf-8”);
- ?>
- <form method=“post” action=“” style=“align:center;”>
- 请输入要生成短地址的url:
- <br />
- <input type=“text” id=“result” name=“url_long”size=“60”value=“”/>
- <input type=“hidden” name=“access_token” value=“1”>
- <input type=“submit” name=“sub” value=“生成”>
- </form>
- <?php
- if($_POST[‘sub’] && $_POST[‘access_token’]=“1”)
- {
- if(preg_match(“#^http://(.*?).(com|net|cc|cn|org|tw|jp)#i“,$_POST[‘url_long’],$a)){
- $url = $_POST[url_long];
- }
- else if(preg_match(“#([a-z0-9]).(com|net|cc|cn|org|tw|jp)#i”,$_POST[‘url_long’],$a)) {
- $url = “http://“.$_POST[url_long];
- }else
- {
- die(“输入有误,非网址,或请加上http://“);
- }
- //echo $url;
- $url = urlencode($url);
- $obj = json_decode(getShort($url),true);
- //var_dump($obj);
- //echo ”<pre>”;
- //print_r($obj);
- //echo ”</pre>”;
- echo “原始地址:”?><a href=“<?php echo $obj[urls][0][url_long];?>”><?php echo $obj[urls][0][url_long];?></a>
- <br />
- 转换后的短地址为:<a href=“<?php echo $obj[urls][0][url_short];?>”><?php echo $obj[urls][0][url_short];?></a>
- <script type=“text/JavaScript”>
- document.getElementById(‘result’).value=“<?php if(isset($obj[urls])) echo $obj[urls][0][url_short];?>”;
- </script>
- <?php
- //var_dump(json_decode(getShort($url)),true);
- }
- function getShort($url)
- {
- $url_long = “https://api.weibo.com/2/short_url/shorten.json?url_long=“.$url.“&access_token=XX”;
- $ch =curl_init($url_long);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $content = curl_exec($ch);
- curl_close($ch);
- return $content;
- }
- ?>