/
Production server management

cURL in PHP - downloading data via URL

15. 02. 2020

Obsah článku

The PHP library `cURL` is a good way to download data from a foreign server.

Based on a query, it builds an HTTP request that it sends to the target server, and once downloaded, it contains an API for (relatively) easy data handling.

Unlike the native file_get_contents function (through which we can also make HTTP requests), it offers much better configuration options and downloads pages/files like a real browser.

The file_get_contents function uses the cURL library internally, it just doesn't have as detailed configuration options.

Detecting cURL mode in a request

It is often useful to detect whether the current request was made via cUrl or classically in the browser.

There is no direct implementation for this in PHP, but we can write a simple function ourselves:

function isCurl(): bool
{
return str_contains($_SERVER['HTTP_USER_AGENT'] ?? '', 'curl');
}

If you have Linux and its Terminal, or are on a Mac, try this command:

curl https://php.baraja.cz/curl

The command makes an internal request to this site and returns the result.

If the application did not detect the cURL request, the HTML will be returned as if the request came from the browser. However, since request types are detected, there is nothing to prevent us from returning a cleaned up Markdown article.

The advantage then is much better cleaned data. We show the formatted HTML to the user in the browser, but only the basic content to the robot.

Detailed use of the API in PHP

If you are looking for detailed usage of cUrl, I recommend reading the official documentation, which is always up to date.

For casual use, there is a Guzzle library that handles

Jan Barášek   Více o autorovi

Autor článku pracuje jako seniorní vývojář a software architekt v Praze. Navrhuje a spravuje velké webové aplikace, které znáte a používáte. Od roku 2009 nabral bohaté zkušenosti, které tímto webem předává dál.

Rád vám pomůžu:

Související články

1.
3.