新浪短网址接口实现

<?php
function mtrand() {
    $str = 'ABCDEFGHIGKLMNOPQRSTUVWXYZ123456789';
    $str = str_shuffle($str);
    $str = substr($str, 0, 8);
    
    return $str;
}
function getMulu() {
    $date = date('Y/m/d/', time());
    
    return $date;
}
function shortenSinaUrl($long_url) {
    $apiKey = '1252439662';
    $apiUrl = 'http://api.t.sina.com.cn/short_url/shorten.json?source=' . $apiKey . $long_url;
    $curlObj = curl_init();
    curl_setopt($curlObj, CURLOPT_URL, $apiUrl);
    curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curlObj, CURLOPT_HEADER, 0);
    curl_setopt($curlObj, CURLOPT_HTTPHEADER, array(
        'Content-type:application/json'
    ));
    $response = curl_exec($curlObj);
    curl_close($curlObj);
    $json = json_decode($response);
    $arr = array();
    foreach ($json as $v) {
        $arr[] = $v->url_short;
    }
    return $arr;
}
function downfile($fileurl) {
    ob_start();
    $filename = $fileurl;
    $date = date("Ymd-H:i:m");
    header("Content-type:  application/octet-stream ");
    header("Accept-Ranges:  bytes ");
    header("Content-Disposition:  attachment;  filename= {$date}.txt");
    $size = readfile($filename);
    header("Accept-Length: " . $size);
}
set_time_limit(0);
if (!empty($_POST['submit'])) {
    if ($_FILES['file']['error'] == 0) {
        if ($_FILES['file']['type'] == 'text/plain') {
            $baseurl = dirname(__FILE__)."/upload/";
            $url = $baseurl . getMulu();
            if (!is_dir($url)) {
                mkdir($url, 0777, true);
            }
            $file_dizhi = $url . mtrand();
            $rs = move_uploaded_file($_FILES["file"]["tmp_name"], $file_dizhi.'.txt');
            if ($rs) {
                //读取文件
                $file = fopen($file_dizhi.'.txt', "r");
                $xieru_txt = '';
                $num = 0;
                $temp_url = '';
                while (!feof($file)) {
                   
                    $temp_url .= '&url_long='.urlencode(trim(fgets($file)));
                    $num++;
                    if($num%20 == 0){
                        $rs =  shortenSinaUrl($temp_url);
                        $xieru_txt .= implode("\r\n", $rs);
                        $xieru_txt .= "\r\n";
                        $temp_url = ''; 
                    }
                    
                }
                fclose($file);
                file_put_contents($file_dizhi.'_xieru.txt', $xieru_txt);
                downfile($file_dizhi.'_xieru.txt');
                exit;
            }
        } else {
            echo '请上传txt文件';
        }
    } else {
        echo '上传失败';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>短网址服务</title>
</head>
<body>
    <form action="" method="post" enctype="multipart/form-data">
        <input type="file" name="file">
        <input type="submit" name="submit" value="提交">
    </form>
</body>
</html>

 

转载于:https://www.cnblogs.com/sbfnxk201/p/6184018.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值