Stop Treating Your API Like a Website
If I see one more developer wrapping XML in a 200 OK response when the database actually failed, I’m going to unplug the rack myself. We are in 2012. The mobile web is exploding. Your iPhone app users on 3G networks in the mountains of Telemark cannot afford the overhead of SOAP envelopes. They need lean, mean JSON responses.
But here is the ugly truth: PHP, out of the box, is not optimized for high-concurrency API delivery. The traditional LAMP stack (Apache + mod_php) creates a process for every request, eating RAM like it's an all-you-can-eat buffet. When your API gets hit by a traffic spike, your server creates swap files until it dies.
In this guide, I’ll show you the architecture we use to serve millions of API requests with sub-100ms latency. We are ditching Apache for Nginx, upgrading to PHP 5.4, and leveraging APC caching.
The Stack: Nginx + PHP-FPM
For high-performance APIs, process management is everything. We rely on Nginx paired with PHP-FPM (FastCGI Process Manager). Unlike Apache, Nginx handles thousands of concurrent connections with a low memory footprint, passing only dynamic PHP requests to the FPM workers.
Here is a production-ready snippet for your nginx.conf to handle API routing without the overhead of `.htaccess` files:
server {
listen 80;
server_name api.yourdomain.no;
root /var/www/api/public;
# Route everything to index.php
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Tweak buffers for JSON payloads
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
}
}
Pro Tip: Notice the fastcgi_buffer settings? If your API returns large JSON collections, default buffers will force Nginx to write to disk (temp files), killing your I/O performance. Keep it in RAM.
PHP 5.4: The New Standard for APIs
If you are still on PHP 5.2, stop reading and upgrade. PHP 5.3 was good, but PHP 5.4 (released earlier this year) is a significant leap forward. It is faster and lighter. Plus, the new short array syntax makes JSON structure definitions much cleaner to read.
Instead of the verbose array(), we can now simply use []. It looks like JSON, it acts like JSON.
// The Old Way (Verbose)
$response = array(
'status' => 'error',
'code' => 404,
'message' => 'Resource not found'
);
// The PHP 5.4 Way (Clean)
$response = [
'status' => 'error',
'code' => 404,
'message' => 'Resource not found'
];
header('Content-Type: application/json');
echo json_encode($response);
Handling HTTP Verbs Correctly
A true REST API respects HTTP verbs. Do not use GET for everything.
- GET for retrieving data.
- POST for creating resources.
- PUT for updating.
- DELETE for removing.
PHP doesn't populate `$_PUT` or `$_DELETE` superglobals automatically. You have to do the dirty work yourself to read the raw input stream:
if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
parse_str(file_get_contents("php://input"), $_PUT);
}
Caching: The APC Accelerator
The fastest code is the code you never execute. For endpoints that don't change often (like product catalogs or region lists), hitting the database every time is negligent.
In 2012, APC (Alternative PHP Cache) is the industry standard for opcode caching and user data caching. It stores PHP variables in shared memory.
$key = 'api_user_1234_stats';
$data = apc_fetch($key);
if ($data === false) {
// Cache miss: Expensive DB query here
$data = $db->query("SELECT * FROM stats WHERE user_id = 1234");
// Store for 60 seconds
apc_store($key, $data, 60);
}
The Infrastructure Factor: Latency & Legal
You can optimize your code all day, but if your server is in a datacenter in Texas and your users are in Oslo, physics will defeat you. Light speed is finite. For a Nordic user base, round-trip time (RTT) across the Atlantic adds 100-150ms of lag before your PHP script even wakes up.
Furthermore, we have the Datatilsynet (Norwegian Data Protection Authority) to worry about. Keeping user data within Norwegian borders or the EEA is not just good for latency; it simplifies your compliance with the Personal Data Act (Personopplysningsloven).
Why I/O Matters More Than CPU
APIs are often I/O bound, not CPU bound. You are waiting for MySQL to find a record, or for logs to be written. This is where standard HDDs fail. High-IOPS environments require SSD RAID storage. In our benchmarks, moving from 15k RPM SAS drives to SSDs reduced API response times by 40% purely due to lower disk wait times.
This is why we built the CoolVDS platform on pure KVM virtualization with SSD storage standard. We don't oversell resources like the budget providers. When you allocate 2GB of RAM, it is yours. No "noisy neighbors" stealing your cycles.
Final Thoughts
Transitioning from SOAP to REST is necessary, but don't underestimate the infrastructure requirements. A slow API breaks mobile apps and frustrates users. By combining the efficiency of Nginx/PHP-FPM, the speed of APC caching, and the raw I/O power of local SSD hosting, you build a system that scales.
Do you need to test your API latency from a local node? Deploy a high-performance SSD instance on CoolVDS in Oslo. Setup takes 55 seconds.