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;
}
} ?>
PHP获取跳转后的URL,存到数据库,设置缓存时间的更多相关文章
- 获取经过跳转后的url地址
粗略一算,不写code已经好几个月了. 昨日受兄弟所托,为他写了一个小小的程序. 程序功能: 自动获取跳转后的Url地址 如下图所示: (newUrl.txt为转换后的地址信息...) 实现过程: 每 ...
- 用Java和Nodejs获取http30X跳转后的url
用Java和Nodejs获取http30X跳转后的url 转 https://calfgz.github.io/blog/2018/05/http-redirect-java-node.html 30 ...
- php获取跳转后的真实链接
网站的跳转链接经常为本站链接加上一些参数来跳转,如何使用php获取跳转后的链接呢? php代码如下: <?php // echo get_redirect_url('http://www.osc ...
- PHP 取302跳转后真实 URL 的两种方法
1 . 第一种,CURL形式[感觉处理略慢,代码偏多] $url = '将跳转的URL'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url) ...
- 使用curl获取Location:重定向后url
在php获取http头部信息上,php有个自带的函数get_headers(),我以前也是用这个的,听说效率在win上不咋地,再加上最近研究百度url无果,写了cURL获取重定向url的php代码来折 ...
- php 使用curl获取Location:重定向后url
在php获取http头部信息上,php有个自带的函数get_headers(),我以前也是用这个的,听说效率在win上不咋地,再加上最近研究百度url无果,写了cURL获取重定向url的php代码来折 ...
- 关于使用struts2跳转后css和js失效的解决方式
根据观察,主要是由于通过action跳转后的url会根据命名空间,自动跳转到命名空间子目录,使得当前引用的css和js查找不到,从而失效,根据这个原因,可使用四种办法解决: 1.使用struts2.x ...
- redis基本操作,基于StringRedisTemplate,存储,取值,设置超时时间,获取超时时间,插入list操作
@Autowired private StringRedisTemplate stringRedisTemplate; @GetMapping("/test") void test ...
- Vue Router路由守卫妙用:异步获取数据成功后再进行路由跳转并传递数据,失败则不进行跳转
问题引入 试想这样一个业务场景: 在用户输入数据,点击提交按钮后,这时发起了ajax请求,如果请求成功, 则跳转到详情页面并展示详情数据,失败则不跳转到详情页面,只是在当前页面给出错误消息. 难点所在 ...
随机推荐
- Unity 3D换装系统教程/Demo
Unity3D换装系统教程 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 1 Costume Change ...
- Prime Ring Problem dfs
A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle ...
- java反射+java泛型,封装BaseDaoUtil类。供应多个不同Dao使用
当项目是ssh框架时,每一个Action会对应一个Service和一个Dao.但是所有的Ation对应的Dao中的方法是相同的,只是要查的表不一样.由于封装的思想,为了提高代码的重用性.可以使用jav ...
- CH4101 银河英雄传说
题意 4101 银河英雄传说 0x40「数据结构进阶」例题 描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七 ...
- 模板引擎之jade 学习
jade 模板引擎在node express 开发中有较多的使用,首先我们看一个简单的使用jade 生成的html 页面的标签代码: doctype html html(lang="en&q ...
- 遍历json 对象的属性并且动态添加属性
昨天因为公司的一个需求,所以就研究了一下json对象的属性的遍历和动态修改: var person= { name: 'zhangsan', pass: '123' , 'sni.ni' : 'sss ...
- linux下编译GD(freetype+libjpeg+libpng+gd-devel)
linux下编译GD(freetype+libjpeg+libpng+gd-devel) 1.检查freetype是否安装rpm -qa | grep freetype没有的话编译freetype 这 ...
- gitlab怎么删除创建的项目
在gitlab新建了一个项目,怎么将此项目删除呢?打开这个工程,点击右上角的“settings”拉到最下面,有个show them to me,点击~在下拉选项的最后,有个remove框,点击即可以彻 ...
- JQuery 实现 Tab 切换 index
$(function(){ var h_new=$('.new h4 a'); var new_dl=$('.new dl'); new_dl.first().show(); h_new.mousee ...
- <<APUE>> 编译方法
/********************************************************************************第0种-最简单实用********** ...