<?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;
}
} ?>

PHP获取跳转后的URL,存到数据库,设置缓存时间的更多相关文章

  1. 获取经过跳转后的url地址

    粗略一算,不写code已经好几个月了. 昨日受兄弟所托,为他写了一个小小的程序. 程序功能: 自动获取跳转后的Url地址 如下图所示: (newUrl.txt为转换后的地址信息...) 实现过程: 每 ...

  2. 用Java和Nodejs获取http30X跳转后的url

    用Java和Nodejs获取http30X跳转后的url 转 https://calfgz.github.io/blog/2018/05/http-redirect-java-node.html 30 ...

  3. php获取跳转后的真实链接

    网站的跳转链接经常为本站链接加上一些参数来跳转,如何使用php获取跳转后的链接呢? php代码如下: <?php // echo get_redirect_url('http://www.osc ...

  4. PHP 取302跳转后真实 URL 的两种方法

    1 . 第一种,CURL形式[感觉处理略慢,代码偏多] $url = '将跳转的URL'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url) ...

  5. 使用curl获取Location:重定向后url

    在php获取http头部信息上,php有个自带的函数get_headers(),我以前也是用这个的,听说效率在win上不咋地,再加上最近研究百度url无果,写了cURL获取重定向url的php代码来折 ...

  6. php 使用curl获取Location:重定向后url

    在php获取http头部信息上,php有个自带的函数get_headers(),我以前也是用这个的,听说效率在win上不咋地,再加上最近研究百度url无果,写了cURL获取重定向url的php代码来折 ...

  7. 关于使用struts2跳转后css和js失效的解决方式

    根据观察,主要是由于通过action跳转后的url会根据命名空间,自动跳转到命名空间子目录,使得当前引用的css和js查找不到,从而失效,根据这个原因,可使用四种办法解决: 1.使用struts2.x ...

  8. redis基本操作,基于StringRedisTemplate,存储,取值,设置超时时间,获取超时时间,插入list操作

    @Autowired private StringRedisTemplate stringRedisTemplate; @GetMapping("/test") void test ...

  9. Vue Router路由守卫妙用:异步获取数据成功后再进行路由跳转并传递数据,失败则不进行跳转

    问题引入 试想这样一个业务场景: 在用户输入数据,点击提交按钮后,这时发起了ajax请求,如果请求成功, 则跳转到详情页面并展示详情数据,失败则不跳转到详情页面,只是在当前页面给出错误消息. 难点所在 ...

随机推荐

  1. CentOS7使用打开关闭防火墙与端口

    systemctl是CentOS7的服务管理工具中主要的工具,它融合之前service和chkconfig的功能于一体. 启动一个服务:systemctl start firewalld.servic ...

  2. gcd模板(欧几里得与扩展欧几里得、拓展欧几里得求逆元)

    gcd(欧几里得算法辗转相除法): gcd ( a , b )= d : 即 d = gcd ( a , b ) = gcd ( b , a mod b ):以此式进行递归即可. 之前一直愚蠢地以为辗 ...

  3. Microsoft-Excel Sheet Column Number

    Given a column title as appear in an Excel sheet, return its corresponding column number. For exampl ...

  4. Django FBV和CBV -

    一.FBV和CBV 在Python菜鸟之路:Django 路由.模板.Model(ORM)一节中,已经介绍了几种路由的写法及对应关系,那种写法可以称之为FBV: function base view ...

  5. day36 python学习gevent io 多路复用 socketserver *****

    ---恢复内容开始--- gevent 1.切换+保存状态 2.检测单线程下任务的IO,实现遇到IO自动切换 Gevent 是一个第三方库,可以轻松通过gevent实现并发同步或异步编程,在geven ...

  6. 万年历(hao123)代码

    网上有很多类似的代码,比如网站:hao123,IP138.日梭万年历网络版 等等,不过日梭万年历相对比较详细,也可以看看另外一篇文章的介绍: 日梭万年历网络版:http://www.cnblogs.c ...

  7. 使用scrapy框架爬取自己的博文(3)

    既然如此,何不再抓一抓网页的文字内容呢? 谷歌浏览器有个审查元素的功能,就是按树的结构查看html的组织形式,如图: 这样已经比较明显了,博客的正文内容主要在div 的class = cnblogs_ ...

  8. WCF揭秘学习笔记(5):WF定制活动

    WF(Windows Workflow Foundation,Windows工作流基础)为.NET提供了一种基于模型的.声明方式的过程执行引擎,它改变了传统的通过一行行编写代码来开发服务功能的方式. ...

  9. VS起始页不显示最近使用的项目解决方案

    前段时间换了一家公司,做ASP.NET开发,让我郁闷的是VS的起始页总是不显示最近使用项目,起先没在意,后来觉得越来越不方便了,然后本着内事不决问百度,外事不决问谷歌的态度,我就百了下~,结果还真遇到 ...

  10. win8设置开机启动项

    c:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp