Skip to content

Instantly share code, notes, and snippets.

@sarim
Created February 5, 2014 06:48
Show Gist options
  • Save sarim/8818486 to your computer and use it in GitHub Desktop.
Save sarim/8818486 to your computer and use it in GitHub Desktop.
server {
listen 80;
server_name fonts.googleapis.com themes.googleusercontent.com;
root /Users/sarim/Sites/php55/;
index app_dev.php index.html index.htm;
location / {
fastcgi_pass unix:/usr/local/var/run/php55.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/googlefont.php;
include fastcgi_params;
}
}
<?php
$url = 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$proxy = array(
'themes.googleusercontent.com' => '173.194.36.43:80',
'fonts.googleapis.com' => "74.125.200.95:80");
$mysqli = new mysqli('localhost','dev','dev','googlefont');
$query = "SELECT * FROM googlefont WHERE url='" . $mysqli->real_escape_string($url) . "'";
if($result = $mysqli->query($query)){
if($row = $result->fetch_object()){
header('Content-Type: '.$row->mimetype);
echo $row->data;
} else {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROXY, $proxy[$_SERVER["HTTP_HOST"]]);
$data = curl_exec ($ch);
$mimetype = curl_getinfo($ch,CURLINFO_CONTENT_TYPE);
curl_close ($ch);
$stmt = $mysqli->prepare("INSERT INTO googlefont (url,mimetype,data) VALUES(?,?,?)");
$null = NULL;
$stmt->bind_param("ssb", $url , $mimetype, $null);
$stmt->send_long_data(2,$data);
$stmt->execute();
header('Content-Type: '.$mimetype);
echo $data;
}
$result->close();
}
$mysqli->close();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment