PHP file_get_contents very slow when using full url
When a script that used to generate PDFs from HTML suddenly starts taking 60â90 seconds instead of milliseconds, the bottleneck is almost always network I/O rather than CPU or memory. In your specific case, the slowdown isolates cleanly to file_get_contents when called with a full URL versus a local path. The difference isn't magic; it's the fact that the first call hits the filesystem and the second one must traverse DNS, establish a TCP handshake, perform TLS negotiation (if HTTPS), and wait for remote latency and bandwidth.
Local files bypass all of that. A read from /var/www/test.txt is typically limited only by disk speed and PHP's internal buffering. By contrast, fetching http://example.com/test.txt requires the request to leave your server, wait for a round-trip to the remote host, and handle whatever congestion or rate limiting exists on the path. That alone can add tens of seconds if the remote server is slow to respond or if there's high network latency.
The symptom you're seeingâslow behavior with any external URL, including http://www.google.comâconfirms that the issue is not specific to a particular domain but to the HTTP fetch mechanism itself. The fact that fopen, readfile, and curl behave the same way points to PHP's default HTTP client: cURL via libcurl. When you call file_get_contents('http://...'), PHP internally invokes cURL unless you've configured a custom handler.
Why this is slow now
If performance changed within weeks, one or more of these factors likely shifted:
- DNS resolution: If your server's DNS cache is stale or your nameserver is misconfigured, each request may incur extra lookup time.
- TLS handshake overhead: HTTPS connections require a full TLS handshake, which can be slow on high-latency networks or with weak cipher suites.
- Remote server behavior: The target server might have introduced rate limiting, increased TTFB (time to first byte), or moved endpoints behind load balancers that add latency.
- PHP/cURL configuration changes: System-wide settings like
curl.max_filesize, timeout values, or connection reuse can affect performance. PHP also reuses persistent connections when configured correctly; disabling them forces a new handshake per request.
You mentioned query string variables are necessary. That's common: many APIs require parameters that only make sense via HTTP. But those parameters don't change the fact that you're hitting the network every time.
Practical diagnostics
Before optimizing, confirm where time is spent:
$startTime = microtime(true);
$content = file_get_contents('http://example.com/test.txt');
$endTime = microtime(true);
echo 'Total: ' . ($endTime - $startTime) . "s\n";
if (isset($HTTP_REQUEST_TIME)) {
echo 'TTFB: ' . $HTTP_REQUEST_TIME . "s\n";
}
Note that file_get_contents does not expose TTFB directly; you'll need to inspect cURL's info via curl_getinfo() when using cURL explicitly. Wrap the call in a cURL handle to get finer metrics:
$ch = curl_init('http://example.com/test.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, false); // we want the body
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$start = microtime(true);
$response = curl_exec($ch);
$elapsed = microtime(true) - $start;
$info = curl_getinfo($ch);
echo "Total time: {$elapsed}s\n";
echo "TTFB: {$info['total_time']}s\n";
echo "Content-Type: {$info['content_type']}\n";
curl_close($ch);
Use this to separate DNS, connect, TLS handshake, and actual data transfer times. If TTFB is huge, the remote endpoint or network path is the bottleneck. If total_time dominates after a small TTFB, you're likely fetching large payloads over a slow link.
Making HTTP fetches faster
If you control the environment, consider these improvements:
Enable persistent connections
SetCURLOPT_FOLLOWLOCATION,CURLOPT_RETURNTRANSFER, and ensureCURLOPT_HTTP_VERSIONis compatible with your server's cURL build. Persistent connections allow PHP to reuse TCP/TLS sockets across requests, drastically reducing handshake overhead when calling multiple URLs in a loop or script.Tune timeouts and buffers
AdjustCURLOPT_TIMEOUT,CURLOPT_CONNECTTIMEOUT, andCURLOPT_BUFFERSIZEto match your expected payload sizes. Avoid excessively large timeouts that mask failures while inflating perceived latency.Prefer HTTPS with strong ciphers
Modern servers and clients negotiate faster handshakes when using TLS 1.3 and well-configured cipher suites. If you're stuck on HTTP for legacy reasons, consider migrating to HTTPS where possible.Use a dedicated HTTP client in long-running scripts
For complex workflows (e.g., generating PDFs from many pages), build a small HTTP facade around cURL that manages connection pooling and retries. This gives you more control than relying onfile_get_contentsalone.
When to move away from file_get_contents
If your workflow involves many sequential fetchesâlike scraping multiple URLs before generating a batch of PDFsâreplacing file_get_contents with an explicit cURL wrapper or a library like Guzzle will yield better performance and observability. You can keep the same query-string logic but gain visibility into each step of the request lifecycle.
There's no single "fix" that turns 90-second HTTP fetches into instant ones if the remote side is slow. But understanding that file_get_contents with a full URL is inherently network-bound lets you diagnose whether the problem lies in your configuration, your network path, or the upstream serverâand act accordingly.