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请求,如果请求成功, 则跳转到详情页面并展示详情数据,失败则不跳转到详情页面,只是在当前页面给出错误消息. 难点所在 ...
随机推荐
- java正则表达式——Greedy、Reluctant和Possessive
测试1: package jichu; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Mai ...
- 租酥雨的NOIP2018赛前日记
租酥雨的NOIP2018赛前日记 离\(\mbox{NOIP2018}\)只剩下不到一个月的时间辣! 想想自己再过一个月就要退役了,觉得有必要把这段时间的一些计划与安排记录下来. 就从国庆收假开始吧. ...
- java泛型学习(1)
java泛型(Generices Type) --->概念:泛型是Java SE 1.5的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数.这种参数类型可以用在类.接口和 ...
- windows 版 nginx 运行错误的一些解决方法
1. 关于文件夹的中文的问题. 错误的截图如下: 看得到这个 failed (1113: No mapping for the Unicode character exists in the targ ...
- Map 的营救;使对象属性有顺序
使用ES6的新特性Map.Map 对象以插入的顺序遍历元素.for...of循环为每一次循环返回一个[key, value]数组. 如果想在跨浏览器环境中模拟一个有序的关联数组,你要么使用两个分开的数 ...
- bzoj1072排列
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1072 好像是这方面的裸题. 整除k 要想转移需要记录下 达到模k所有余数 的方案数. 为了生 ...
- VS2010安装选项中有个“图标库”
位置 :\Program Files\Microsoft Visual Studio 10.0\Common7\VS2010ImageLibrary\2052\VS2010ImageLibrary.z ...
- Hbuilder mui 相册拍照图片上传
http://www.bcty365.com/content-146-3648-1.html 使用流程 弹出actionSheet /*点击头像触发*/ document.getElementById ...
- C#实现不安装Oracle客户端访问远程服务器数据
概述: C#通过使用ADO的方式在未安装Oracle数据库的前提下,客户端程序远程访问服务器,会出现:“System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或 ...
- linux 线程的同步 一 (互斥量和信号量)
互斥量(Mutex) 互斥量表现互斥现象的数据结构,也被当作二元信号灯.一个互斥基本上是一个多任务敏感的二元信号,它能用作同步多任务的行为,它常用作保护从中断来的临界段代码并且在共享同步使用的资源. ...