PHP Non-blocking fopen, file_get_contents using stream_set_blocking

Here is a php script that calls a urls without blocking and returns immediately.

function call_url($url) {
    // Call the url and return immediately with the length of the data read.
    // Useful for firing off scripts by calling a url.

    $fp = fopen($url, 'r');
    stream_set_blocking($fp, 0); // 0 => non-blocking mode
    $data = fread($fp, 8192); // 1024 * 8 = 8192
    fclose($fp);

    // Return the amount of data read
    return strlen($data);
}

1 comment

  1. anonymous

    Thank you!

Leave a Reply