Created
May 22, 2018 02:14
-
-
Save x7hub/11d9c78696d8cd7c3cfb9b14cf701284 to your computer and use it in GitHub Desktop.
Redirect page based on cowboy/php-simple-proxy
This file contains 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 | |
// Redirect page based on cowboy/php-simple-proxy | |
// | |
// License: php-simple-proxy | |
// | |
// Copyright (c) 2010 "Cowboy" Ben Alman, | |
// Dual licensed under the MIT and GPL licenses. | |
// http://benalman.com/about/license/ | |
// | |
// ############################################################################ | |
$des_host = 'x7blog.info'; | |
$current_host = 'x7blog.000webhostapp.com'; | |
$url = 'https://' . $des_host . $_SERVER['REQUEST_URI']; | |
if (!preg_match("/baiduspider/i", $_SERVER['HTTP_USER_AGENT'])) { | |
header('HTTP/1.1 301 Moved Permanently'); | |
header('Location: ' . $url); | |
exit(); | |
} | |
$user_agent = preg_replace('/baiduspider/i', 'php-simple-proxy', $_SERVER['HTTP_USER_AGENT']); | |
// ############################################################################ | |
$ch = curl_init( $url ); | |
if ( strtolower($_SERVER['REQUEST_METHOD']) == 'post' ) { | |
curl_setopt( $ch, CURLOPT_POST, true ); | |
curl_setopt( $ch, CURLOPT_POSTFIELDS, $_POST ); | |
} | |
// send cookies | |
$cookie = array(); | |
foreach ( $_COOKIE as $key => $value ) { | |
$cookie[] = $key . '=' . $value; | |
} | |
$cookie = implode( '; ', $cookie ); | |
curl_setopt( $ch, CURLOPT_COOKIE, $cookie ); | |
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true ); | |
curl_setopt( $ch, CURLOPT_HEADER, true ); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); | |
curl_setopt( $ch, CURLOPT_USERAGENT, $user_agent ); | |
list( $header, $contents ) = preg_split( '/([\r\n][\r\n])\\1/', curl_exec( $ch ), 2 ); | |
$status = curl_getinfo( $ch ); | |
curl_close( $ch ); | |
// Split header text into an array. | |
$header_text = preg_split( '/[\r\n]+/', $header ); | |
// Propagate headers to response. | |
foreach ( $header_text as $header ) { | |
if ( preg_match( '/^(?:Content-Type|Content-Language|Set-Cookie):/i', $header ) ) { | |
//header( $header ); | |
header(str_replace($des_host, $current_host, $header)); | |
} | |
} | |
print str_replace($des_host, $current_host, $contents); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
通过代理帮助百度爬虫读取github pages网站的内容