function main(&$stop) { echo "Started first requests...\n"; var_dump(array_map('strlen', (yield ['Content-Length of github.com' => curl_init_with('https://github.com/mpyw'), 'Content-Length of twitter.com' => curl_init_with('https://twitter.com/mpyw')]))); echo "Started second requests...\n"; var_dump(array_map('strlen', (yield ['Content-Length of example.com' => curl_init_with('http://example.com')]))); $stop = true; }
function download_image_async(string $url, string $basename, string $savedir = '/tmp') : \Generator { static $exts = ['image/jpeg' => '.jpg', 'image/png' => '.png', 'image/gif' => '.gif']; $content = (yield curl_init_with($url)); $finfo = new finfo(FILEINFO_MIME_TYPE); $type = $finfo->buffer($content); $ext = isset($exts[$type]) ? $exts[$type] : ''; file_put_contents("{$savedir}/{$basename}{$ext}", $content); echo "Downloaded {$url}, saved as {$savedir}/{$basename}{$ext}\n"; }
function get_xpath_async($url) { $dom = new \DOMDocument(); @$dom->loadHTML((yield curl_init_with($url))); (yield Co::RETURN_WITH => new \DOMXPath($dom)); }
function get_xpath_async(string $url) : \Generator { $dom = new \DOMDocument(); @$dom->loadHTML((yield curl_init_with($url))); return new \DOMXPath($dom); }