A Swiss Army Knife PHP Script

1 minute read

When the Chinese government blocked foursquare.com, I downloaded a tiny zip file, in which there were only a handful of php and apache config files. It made my 4sq app on android cellphone work again. I skimmed through the source code. It’s pretty simple, just a comple lines to implement the request redirect function.

I found my Twip proxy for Twitter didn’t work anymore. It’s already the latest version. I looked like there was little I could do. And suddennly the 4sq proxy came to my mind. Hey, maybe I could modify it and use it for twitter.

And it did work. All I needed to do is just change the value of the api url to twitter’s url, and config a subdomain for it. It worked breezily!

Here is the source code of the two main important files:

  1. .htaccess (the apache config file which lets 4sq.php process all the request):
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . 4sq.php [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
  1. 4sq.php (grabs the request, and redirects them to the real receiver):

<?php
$file_name = ‘4sq.php’;
$api_root = ‘http://api.twitter.com/’;
$request_uri = $_SERVER[‘REQUEST_URI’];
$pos = strpos($_SERVER[‘SCRIPT_NAME’], $file_name);
$path_component = substr($_SERVER[‘REQUEST_URI’], $pos);
$curl = curl_init();
$headers[] = ‘Connection: Keep-Alive’;
$headers[] = ‘User-Agent: FxxkGFW’;
$username = $_SERVER[‘PHP_AUTH_USER’];
$password = $_SERVER[‘PHP_AUTH_PW’];
curl_setopt_array($curl, array(
CURLOPT_URL => $api_root . $path_component,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_USERPWD => “$username:$password”)
);
if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’) {
curl_setopt_array($curl, array(
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => @file_get_contents(‘php://input’)
));
}
$result = curl_exec($curl);
curl_close($curl);
?>

Categories: Internet, Tech

Updated: