Various APIs (application programming interfaces) are meant to significantly ease the life of both the developers and users of web resources. They are able to solve complex problems, provide communication between programs, and automate any processes. We have detailed a number of famous APIs in Drupal: Database API, Queue API, Batch API, Field API, Ctools modal API, etc. Today, let's talk about one specific mission that can be successfully entrusted to APIs: receiving data from a website in the form of handy CSV reports.
The CSV format: essence and benefits
CSV is a popular format widely used for importing and exporting data. Its name stands for Comma-Separated Values, which mostly describes the essence of its work. Tabular data is stored as plain text, where the values are separated by commas, and the lines by newline characters. Each value corresponds to one cell on the table and each line to one table row, respectively.
CSV reports look very concise. A CSV file’s size can be several times smaller than that of an XML file, which is a very efficient solution. Even the database of a big online store can fit in 1-2 megabytes. However, it should be noted that XML is somewhat ahead of CSV in terms of complex formatting and styling opportunities.
By default, a CSV file can be viewed in the notepad editor. In addition, it can be opened in Excel, Google Spreadsheets and similar applications, as well as converted to other formats for optimal information presentation.
A special tool created by InternetDevels: an API to generate CSV reports
For convenient CSV data export, special software solutions are created. Even if your site operates with a huge amount of information, it is always possible to create an easy withdrawal of all relevant data. This applies not only to your own website — you can also export data from other sites to which you have access, or which are not closed.
We already have experience in creating an API to retrieve data from the site, with the further generation of CSV reports on the basis of this data. It’s been done for the website of a large platform in the real estate industry that aggregates data from many different sources, and offers opportunities for data processing, systematization, and conversion into user-friendly formats. This kind of functionality was essential to do that.
So let's see how this API works. We can get CSV reports in several steps: sending a request to the server, getting a response in the JSON format, converting data to CSV and saving it in a file.
Requesting the data
So first we need to send a GET request to the server. This request consists of two parts:
- the path to the site that always remains the same (when dealing with the same site);
- additional options that allow us to fully customize our CSV reports so they only contain the information we are interested in
Additional options are field and filters. Let’s consider them in more detail.
- A handy pop-up will show us a list of fields, among which we can select and check the ones we need. These will be the names of the future columns in our CSV table (e.g., price, ID, address, etc.)
- In addition, we can configure the filters by specifying the criteria to sort the data out. Then we will only get the data that falls under specified criteria (for example, objects in the specified range of prices).
Getting the data, converting it to CSV and saving it in a file
"Telling" the server what we want to see in the table in every detail, we get a response in the JSON format to our request. The data is analyzed at the program level and converted to the CSV format, and then saved in a file. This file can be easily downloaded and used as a basis for handy reports in any popular formats — for example, presented as HTML on a website and so on.
For the quick retrieval of statistics, our API works with the powerful Solr service, which stores, indexes and quickly finds the necessary information.
We will gladly create the functionality for CSV reports or anything else that will open new opportunities for your web resource. Tell us about your needs and ideas — and the perfect software solution will not keep on coming!