• PHP获取跳转后的URL,存到数据库,设置缓存时间


    <?php
    error_reporting(0);
    header("Content-Type: text/html; charset=utf-8"); 
    $fid=$_GET["fid"];
    $fid=3147111187646345;
    $limittime='600';//单位秒    
    $accessToken='b452d6669d7d41efabd92190c1f1c2d9'; //这里填写你自己帐号捉包的accessToken
    
    //连接数据库查询FID是否有数据,并且是否有效
     
     
     //表自己建吧,就几个自动
    
    $btime=$limittime*1000;//获取毫秒数
    $stime=time();//开始时间
    $etime=time()+$btime;//结束时间
        
    //查询下FID是否有数据
    $sql="select count(*) as total from url_table where fid='".$fid."' and down_url!='http:&ctyunapi.cn'";
    $result=mysql_query($sql);
    $data=mysql_fetch_array($result);
    $total=$data[0]['total'];
    
    if($total>0){
        //如果有数据,校验下,是否有效
        $sql="select count(*) as total  from url_table where fid='".$fid."' and stime>=".$btime." and etime<=".$btime;
        $result=mysql_query($sql);
        $data=mysql_fetch_array($result);
        $totalt=$data[0]['total'];
        
        if($totalt<1){
            //更新获取的数据
            //获取下载地址
        
            $data=get_curl_contents('https://api.cloud.189.cn/loginByOpen189AccessToken.action?accessToken='.$accessToken);
            preg_match('|sessionKey>(.*)</sessionKey|U',$data,$a);
            preg_match('|sessionSecret>(.*)</sessionSecret|U',$data,$b);
            $data=get_curl_contents('https://cloud.189.cn/v2/getFileInfo.action?fileId='.$fid.'&sessionKey='.$a[1].'&sessionSecret='.$b[1]);
    
            $json=json_decode($data,true); 
            $downloadUrl = 'http:'.$json['downloadUrl'];
            $videoUrl = 'http:'.$json['videoUrl'];
    
            $html=get_curl_contents($videoUrl);
            $jsons=json_decode($html,true); 
            $video = $jsons['url'];
    
            if (!isset($video)) {
                $videos=$downloadUrl.'&ctyunapi.cn';
            } else {
                $videos='http:'.getrealurl($video).'&ctyunapi.cn';
            }
            $down_url=getrealurl($videos);
        
            $sql="update url_table set down_url='".$down_url."',stime=".$stime.",etime=".$etime." where fid=".$fid;
            mysql_query($sql);
        }  else {
    
            $sql="select down_url  from url_table where fid='".$fid."' and stime>=".$btime." and etime<=".$btime;
            $result=mysql_query($sql);
            $data=mysql_fetch_array($result);
            $down_url=$data[0]['down_url'];
        }
    } else {
        //直接插入数据
             
            $data=get_curl_contents('https://api.cloud.189.cn/loginByOpen189AccessToken.action?accessToken='.$accessToken);
            preg_match('|sessionKey>(.*)</sessionKey|U',$data,$a);
            preg_match('|sessionSecret>(.*)</sessionSecret|U',$data,$b);
            $data=get_curl_contents('https://cloud.189.cn/v2/getFileInfo.action?fileId='.$fid.'&sessionKey='.$a[1].'&sessionSecret='.$b[1]);
    
            $json=json_decode($data,true); 
            $downloadUrl = 'http:'.$json['downloadUrl'];
            $videoUrl = 'http:'.$json['videoUrl'];
    
            $html=get_curl_contents($videoUrl);
            $jsons=json_decode($html,true); 
            $video = $jsons['url'];
    
            if (!isset($video)) {
                $videos=$downloadUrl.'&ctyunapi.cn';
            } else {
                $videos='http:'.getrealurl($video).'&ctyunapi.cn';
            }
            $down_url=getrealurl($videos);
         
     
        $sql="insert into url_table(fid,stime,etime,down_url)values('".$fid."',".$stime.",".$etime.",'".$down_url."')";
        mysql_query($sql);
    }
    
    mysql_close($conn);
    
    header("Location:$down_url");    
    
     
    
    
    function get_curl_contents($url,$header=0,$nobody=0){
            if(!function_exists('curl_init')) die('php.ini未开启php_curl.dll');
            $c = curl_init();
            curl_setopt($c, CURLOPT_URL, $url);
            curl_setopt($c, CURLOPT_HEADER, $header);
            curl_setopt($c, CURLOPT_NOBODY, $nobody);
            curl_setopt($c, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($c, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($c, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
            curl_setopt($c, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'.$_SERVER["REMOTE_ADDR"], 'CLIENT-IP:'.$_SERVER["REMOTE_ADDR"]));
            $content = curl_exec($c);
            curl_close($c);
        return $content;    
    }
    
    function getrealurl($url){
        $header = get_headers($url,1);
        if (strpos($header[0],'301') || strpos($header[0],'302')) {
            if(is_array($header['Location'])) {
                return $header['Location'][count($header['Location'])-1];
            }else{
                return $header['Location'];
            }
        }else {
            return $url;
        }
    }
    
    ?>
  • 相关阅读:
    隐性改变display类型
    垂直居中-父元素高度确定的多行文本(方法二)
    去掉WIN7 桌面图标的小箭头
    搭建高可用mongodb集群(三)—— 深入副本集内部机制
    搭建高可用mongodb集群(二)—— 副本集
    搭建高可用mongodb集群(一)——配置mongodb
    Linux:Tomcat报错: Error creating bean with name 'mapScheduler' defined in ServletContext resource 的解决方法
    LINUX ORACLE 启动与关闭
    Linux 安装 Oracle 11g R2
    ORACLE 数据库优化原则
  • 原文地址:https://www.cnblogs.com/baker95935/p/7715941.html
Copyright © 2020-2023  润新知