Writing CURLObservable
As we said, we're going to work with API calls and, for this reason, we need a comfortable way of creating HTTP requests. It's probably no surprise that we'll write a custom Observable that downloads a URL and passes it's response to its observers, where we'll decode it from JSON using the operator we created just a couple of lines above.
We're going to use PHP's cURL module, which is a wrapper around libcurl ( https://curl.haxx.se/libcurl/ ) - a C library for transferring data via any protocols imaginable.
We'll start by using plain simple cURL in PHP and we'll see that it supports some sort of asynchronous approach out-of-the-box.
Imperative approach and cURL
If we just wanted to download a single URL, we wouldn't need anything special. However, we want to make this, and all future applications of CURLObservable
class, more interactive, so we'll also keep track of the downloading progress.
A plain and simple approach could look like this:
// curl_01.php $ch = curl_init...