C/PHP: How do I convert the following PHP JSON API script into a C plugin for apache?
Posted
by TeddyB
on Stack Overflow
See other posts from Stack Overflow
or by TeddyB
Published on 2010-04-28T22:32:37Z
Indexed on
2010/04/28
22:37 UTC
Read the original article
Hit count: 207
I have a JSON API that I need to provide super fast access to my data through.
The JSON API makes a simply query against the database based on the GET parameters provided.
I've already optimized my database, so please don't recommend that as an answer.
I'm using PHP-APC, which helps PHP by saving the bytecode, BUT - for a JSON API that is being called literally dozens of times per second (as indicated by my logs), I need to reduce the massive RAM consumption PHP is consuming ... as well as rewrite my JSON API in a language that execute much faster than PHP.
My code is below. As you can see, is fairly straight forward.
<?php
define(ALLOWED_HTTP_REFERER, 'example.com');
if ( stristr($_SERVER['HTTP_REFERER'], ALLOWED_HTTP_REFERER) ) {
try {
$conn_str = DB . ':host=' . DB_HOST . ';dbname=' . DB_NAME;
$dbh = new PDO($conn_str, DB_USERNAME, DB_PASSWORD);
$params = array();
$sql = 'SELECT homes.home_id,
address,
city,
state,
zip
FROM homes
WHERE homes.display_status = true
AND homes.geolat BETWEEN :geolatLowBound AND :geolatHighBound
AND homes.geolng BETWEEN :geolngLowBound AND :geolngHighBound';
$params[':geolatLowBound'] = $_GET['geolatLowBound'];
$params[':geolatHighBound'] = $_GET['geolatHighBound'];
$params[':geolngLowBound'] =$_GET['geolngLowBound'];
$params[':geolngHighBound'] = $_GET['geolngHighBound'];
if ( isset($_GET['min_price']) && isset($_GET['max_price']) ) {
$sql = $sql . ' AND homes.price BETWEEN :min_price AND :max_price ';
$params[':min_price'] = $_GET['min_price'];
$params[':max_price'] = $_GET['max_price'];
}
if ( isset($_GET['min_beds']) && isset($_GET['max_beds']) ) {
$sql = $sql . ' AND homes.num_of_beds BETWEEN :min_beds AND :max_beds ';
$params['min_beds'] = $_GET['min_beds'];
$params['max_beds'] = $_GET['max_beds'];
}
if ( isset($_GET['min_sqft']) && isset($_GET['max_sqft']) ) {
$sql = $sql . ' AND homes.sqft BETWEEN :min_sqft AND :max_sqft ';
$params['min_sqft'] = $_GET['min_sqft'];
$params['max_sqft'] = $_GET['max_sqft'];
}
$stmt = $dbh->prepare($sql);
$stmt->execute($params);
$result_set = $stmt->fetchAll(PDO::FETCH_ASSOC);
/* output a JSON representation of the home listing data retrieved */
ob_start("ob_gzhandler"); // compress the output
header('Content-type: text/javascript');
print "{'homes' : ";
array_walk_recursive($result_set, "cleanOutputFromXSS");
print json_encode( $result_set );
print '}';
$dbh = null;
} catch (PDOException $e) {
die('Unable to retreive home listing information');
}
}
function cleanOutputFromXSS(&$value) {
$value = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}
?>
How would I begin converting this PHP code over to C, since C is both better on memory management (since you do it yourself) and much, much faster to execute?
© Stack Overflow or respective owner