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请求,如果请求成功, 则跳转到详情页面并展示详情数据,失败则不跳转到详情页面,只是在当前页面给出错误消息. 难点所在 ...
随机推荐
- Unity3D游戏-愤怒的小鸟游戏源码和教程(一)
Unity愤怒的小鸟游戏教程 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) AngryEva游戏效果: 1 ...
- Tomcat:Several ports are already in use问题
Several ports (8005, 8080, 8009) required by Tomcat v6.0 Server at localhost are already in use. The ...
- 实习第二天-String对象的不可变性-未解决
public class Reverse { public static void main(String[] args) { String c1=new String("abc" ...
- mysql 行号 获取指定行数据
mysql 行号的实现 Select id,(@rowNum:=@rowNum+1) as rowNo From first,(Select (@rowNum :=0) ) bOrder by fir ...
- 使用 php 7.3.0 官网的压缩包安装 FastAdmin
使用 php 7.3.0 官网的压缩包安装 FastAdmin 直接从 php.net 官网下载了一 php7.3.0 的压缩包,解压到 c:/temp/php73 目录. 将 c:/temp/php ...
- 【转】每天一个linux命令(30): chown命令
原文网址:http://www.cnblogs.com/peida/archive/2012/12/04/2800684.html chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者 ...
- netty异步
通俗理解:http://lingnanlu.github.io/2016/08/16/netty-asyc-callback 异步的小demo:https://blog.csdn.net/coder_ ...
- bzoj 2119 股市的预测——枚举长度的关键点+后缀数组
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2119 就是找差分序列上中间差 m 的相等的两段. 考虑枚举这样一段的长度 L .可以把序列分 ...
- Django的 admin管理工具
admin组件使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.py 中的 INSTALLE ...
- AIX6.1用g++安装Poco-1.6.1-all
项目终于到了把程序往小型机上部署的阶段了.不得不说AIX真是让人恶心,一个Poco编译弄了我1周时间.网上根本没有相关的资料. 1. AIX下安装gcc/g++:在ftp://ftp.software ...