Last active
December 21, 2015 01:09
-
-
Save eugeneglova/6225976 to your computer and use it in GitHub Desktop.
php curl proxy
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 | |
/** | |
* proxy.php | |
* | |
* Responsible to get data from remote domain | |
* | |
* mod_rewrite configuration for .htaccess file | |
* | |
* RewriteEngine on | |
* RewriteRule ^$ proxy.php/?$1 [NC,L] | |
* RewriteRule ^index.php$ proxy.php/?$1 [NC,L] | |
* RewriteCond %{REQUEST_FILENAME} !-d | |
* RewriteCond %{REQUEST_FILENAME} !-f | |
* RewriteCond %{REQUEST_FILENAME} !-l | |
* RewriteRule ^(.*)$ proxy.php/?$1 [NC,L] * | |
* | |
* @author Eugene Glova | |
* @since 2013-08-13 | |
* | |
*/ | |
// Local domain | |
$local_domain = $_SERVER['HTTP_HOST']; | |
// Original remote host | |
$remote_domain = 'dev03'; | |
// File to store cookies (must be writable) | |
$cookie_file = 'cookie.txt'; | |
$headers = apache_request_headers(); | |
$headers['Host'] = $remote_domain; | |
$user_agent = $headers['User-Agent']; | |
unset($headers['User-Agent']); | |
// Get data from remote url | |
$ch = curl_init('http://' . $remote_domain . $_SERVER['REQUEST_URI']); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); | |
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); | |
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); | |
// Add support for method POST | |
if ($_SERVER['REQUEST_METHOD'] === 'POST') { | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST); | |
} | |
// Get response | |
$response = curl_exec($ch); | |
// Get http response code | |
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
// Get response content type | |
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); | |
curl_close($ch); | |
// Replace domain name | |
$response = str_replace($remote_domain, $local_domain, $response); | |
// Replace minimized js with development version | |
$response = str_replace('}); | |
</script>', '}); | |
</script> | |
<script src="/js/src/require-config.js"></script> | |
<script> | |
Config.modules.service.connection.types["socket.io"].domain = "node.' . $remote_domain . '"; | |
require_config.baseUrl = "/js/src/"; | |
requirejs.config(require_config); | |
require(["main"]); | |
</script> | |
', $response); | |
// Copy cookies | |
$cookie = file($cookie_file); | |
$prefix = $remote_domain . "\tFALSE\t/\tFALSE\t"; | |
foreach ($cookie as $key => $value) { | |
if (strstr($value, $prefix)) { | |
$cookie_string = trim(str_replace($prefix, "", $value)); | |
$cookie_array = explode("\t", $cookie_string); | |
setcookie($cookie_array[1], $cookie_array[2], $cookie_array[0]); | |
} | |
} | |
// Set response status code | |
header('HTTP/1.0: ' . $http_code); | |
// Set response content type | |
header('Content-type: ' . $content_type); | |
// Output response | |
echo $response; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment