Created
June 14, 2016 05:13
-
-
Save wuliupo/54c7a33346260516db60647613beb0be to your computer and use it in GitHub Desktop.
PHP get remote file, anti anti spam
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$url = getParam('url'); | |
$encode = getParam('encode'); | |
$file = 'cache/'.md5($url); | |
$exist = file_exists($file); | |
if($exist && (time() - filemtime($file) < 3600)){ // use cache during 10 minutes | |
echo file_get_contents($file); | |
exit(); | |
} | |
@list($header, $content) = curlget($url); | |
if(strpos(trim($content), '<script>') === 0){ | |
$i = 0; | |
do { | |
list($header, $content) = curlget($url); | |
$ret = preg_match('/\)\(\[\'[^)]+\)/', $content, $matchs); | |
if(++$i == 3) break; | |
if(!$ret) { | |
sleep(1); | |
} | |
} while(!$ret); | |
if($ret){ | |
$str = trim($matchs[0], '()[]\''); | |
$arr = explode("', '", $str); | |
preg_match('/b d=\[([^\]]+)/', $content, $matchs); | |
$sort = explode(',', $matchs[1]); | |
$str = ''; | |
foreach($sort as $i){ | |
$str .= $arr[$i]; | |
} | |
if(strpos($str, '__jsl_clearance') !== 0){ | |
$arr2 = array(); | |
for($i = count($sort); $i--;){ | |
$arr2[$sort[$i]] = $arr[$i]; | |
} | |
ksort($arr2); | |
$str = implode($arr2); | |
} | |
$tmp = explode(';', $str); | |
$cookie = trim($tmp[0]); //get cookie from JS | |
preg_match('/Set-Cookie:(.*);/iU', $header, $str); | |
$cookie .= '; '.$str[1]; // append the old cookie | |
list($header, $content) = curlget($url, $cookie); | |
} else { | |
$content = ''; | |
} | |
} | |
if(!empty($content)){ | |
if(!empty($encode) && !isImg($file)){ | |
$content = iconv($encode, 'UTF-8', $content); // change given encode (GBK, GB2312) to utf8 | |
} | |
file_put_contents($file, $content); | |
echo $content; | |
exit(); | |
} else if($exist){ | |
echo file_get_contents($file); | |
exit(); | |
} | |
function curlget($url, $cookie = ''){ | |
$cookieFile = 'cache/cookie'; | |
if($cookie) { | |
file_put_contents($cookieFile, $cookie); | |
} else if (file_exists($cookieFile) && (time() - filemtime($cookieFile) < 3600)){ | |
$cookie = file_get_contents($cookieFile); | |
} | |
$ch = curl_init(); | |
$timeout = 5; | |
$user_agent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36'; | |
$headers = array( | |
'Accept-Encoding: gzip, deflate, sdch', | |
'Accept-Language: zh-CN,zh;q=0.8', | |
'Upgrade-Insecure-Requests: 1', | |
'User-Agent: '.$user_agent, | |
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', | |
'Referer: '.$url, | |
'Connection: keep-alive', | |
'Cache-Control: max-age=0' | |
); | |
if($cookie) { | |
$headers[] = 'Cookie: '.$cookie; | |
} | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $HTTP_REQUEST_HEADER | |
curl_setopt($ch, CURLOPT_ENCODING, ''); | |
curl_setopt($ch, CURLOPT_HEADER, 1); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | |
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); // $_SERVER['HTTP_USER_AGENT'] | |
curl_setopt($ch, CURLOPT_COOKIESESSION, 1); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); | |
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); | |
$content = curl_exec($ch); | |
curl_close($ch); | |
return explode("\r\n\r\n", $content, 2); | |
} | |
function getParam($key) { | |
$val = $_GET[$key]; | |
return empty($val) ? $_POST[$key] : unescape($val); | |
} | |
function isImg($file) { | |
$subfixs = array('.jpg', '.jpeg', '.png', '.gif'); | |
foreach($subfixs as $subfix){ | |
if (stripos($file, $subfix) !== false) { | |
return true; | |
} | |
} | |
return false; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment