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请求,如果请求成功, 则跳转到详情页面并展示详情数据,失败则不跳转到详情页面,只是在当前页面给出错误消息. 难点所在 ...
随机推荐
- CF1143F/1142C U2
CF1143F/1142C U2 巧妙的思维题.注意到这里只用两个点就可以确定一根抛物线,联想到两点确定一条直线,尝试转化. \(y=x^2+bx+c\) 就可以写成 \(y-x^2=bx+c\) , ...
- 位操作Bit Operation算法题
一道让你拍案叫绝的算法题 这是一道看完答案会觉得很简单,但做之前很难想到答案的题目!!! 不信? Let us go ! 题目描述 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均 ...
- Windows 10中更新Anaconda和第三方包
=============================== 作为专业的Python开发者,Anaconda包肯定很熟悉 下面总结一下Anaconda的升级和维护 步骤一: 打开cmd,切换到Ana ...
- python type metaclass
在python中一切皆对象, 所有类的鼻祖都是type, 也就是所有类都是通过type来创建. 传统创建类 class Foo(object): def __init__(self,name): se ...
- POJ2311 Cutting Game
题意 Language:Default Cutting Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6007 Acc ...
- 使用Spring Boot操作Hive JDBC时,启动时报出错误:NoSuchMethodError: org.eclipse.jetty.servlet.ServletMapping.setDef
使用Spring Boot操作Hive JDBC时,启动时报出错误:NoSuchMethodError: org.eclipse.jetty.servlet.ServletMapping.setDef ...
- SocketIOCP
项目地址 : https://github.com/kelin-xycs/SocketIOCP SocketIOCP 一个 用 C# Socket 实现 的 IOCP 这是一个 用 C# Socke ...
- tomcat源码阅读之Catalina和Bootstrap解析
一.Cataling类分析: 1.Catalina类是启动类,用于启动或者关闭Server对象,它包含一个Digester对象,用于解析tomcat配置文件:conf/server.xml;调用pro ...
- 【RAC】使用一条“ps”命令获取Linux环境下全部RAC集群进程信息
如何仅使用一条ps命令便能获取到所有与RAC集群进程相关的信息. 从所使用的命令角度上看很简单,仅需使用ps命令结合grep命令便能实现.问题关键是需要确定检索哪些关键字. 1.与RAC集群有关的进 ...
- spring initializr 创建项目时,依赖对应的 artifactId
选择各个依赖时,对应的 artifactId 选择的依赖 artifactId名字 不选择依赖,默认包含的artifactId spring-boot-starter.spring-boot-star ...