Treat cURL as a Stream in PHP

class HttpStream {

    public class go($url) {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_WRITEFUNCTION, array($this, 'streamHandler'));
        curl_exec($ch);
        curl_close($ch);
    }

    protected function streamHandler($ch, $data) {
        // Handle the stream here
        // ...
        return strlen($data); // strlen($data) to continue streaming or return another value to end the stream.
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.