Get file content via PHP cURL

Get file content via PHP cURL

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Get file content via PHP cURL

You need to fetch the raw contents of a remote resource, specifically http://www.adserversite.com/ads.php, and render it on your own domain without using an <iframe>. This is a common requirement for displaying external advertisements or embedding third-party widgets directly into the document flow. The standard tool for this in PHP is the cURL extension.

To accomplish this, you must initiate a network request that mimics a browser but captures the response body instead of navigating to the URL. The most robust way to do this is using curl_exec() combined with curl_setopt(). Here is a minimal implementation:

$url = 'http://www.adserversite.com/ads.php';

$ch = curl_init();

// Set the user-agent so the remote server knows this is a browser request, not a bot.
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36');

// Tell cURL to return the content as a string instead of following redirects automatically or printing directly.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Essential if the ad server uses 301/302 redirects.
curl_setopt($ch, CURLOPT_TIMEOUT, 5);          // Prevent hanging indefinitely if the external service is down.

// Optional: Set a custom header if the target site requires it.
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Accept: application/json, text/html'
]);

$response = curl_exec($ch);

if (curl_errno($ch)) {
    // Handle connection errors here (e.g., timeout, DNS failure)
    $error_msg = curl_error($ch);
    echo "cURL Error: $error_msg";
} else {
    echo $response;
}

curl_close($ch);

The critical flag here is CURLOPT_RETURNTRANSFER. Without it, cURL will output the content directly to the browser, which is fine for simple debugging but problematic if you need to manipulate the data before displaying it. By returning the result as a string, you gain full control over how the content is injected into your layout.

Handling Security and Headers

External ad servers often enforce strict security policies. They may reject requests that do not originate from their expected domains or that lack specific headers. If your code attempts to fetch the content and receives an HTTP 403 Forbidden or a "Refused to connect" error, you are likely missing a header requirement.

You can inspect the raw response headers to debug this using curl_getinfo(). Specifically, look for HTTP_CODE to see the status and check if any specific headers were returned that indicate rejection (like X-Frame-Options, though we aren't using iframes here, the server might still block direct fetches based on referrer).

$headers = curl_getinfo($ch);
echo $headers['http_code']; // Check for 200 OK

Another common pitfall is the Referer header. If you are loading an ad from adserversite.com while your main site is domain.com, some ad networks will block the request unless they can verify the referrer matches their own domain. You may need to set CURLOPT_REFERER to mimic a direct visit or configure it according to the ad provider's documentation.

Trade-offs and Limitations

Using cURL to fetch external content introduces significant security considerations. Since you are pulling code from an untrusted third-party server (adserversite.com) and executing it within your own context, you open yourself up to Cross-Site Scripting (XSS) vulnerabilities if the remote file returns JavaScript that executes in your user's browser.

Always sanitize or strictly validate any dynamic content returned by external sources before rendering it to the user. If the remote file is expected to return raw HTML without scripts, consider stripping out <script> tags using a DOM parser before outputting the string.

Modern Alternatives

While cURL is the traditional approach, modern PHP frameworks like Laravel provide built-in helpers that abstract this complexity. The GuzzleHttp client is often preferred for its asynchronous capabilities and better error handling compared to raw cURL. Additionally, since PHP 7.4, the native http_async_wrapper allows you to fetch content asynchronously without blocking the request thread, which is beneficial for high-traffic sites where ad loading shouldn't delay page rendering.

If you require a managed solution that handles these redirects, headers, and security checks automatically, platforms like https://laravelcompany.com offer enterprise-grade services that can manage content delivery securely.

Our network