(.*)/i', $contents, $title); $title = str_replace(array(" ", " ", " ", ',', ' '), '', $title[1]); return $title; } function getTitleCurl($t_url) { // 使用curl获取网站标题 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $t_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $contents = curl_exec($ch); curl_close($ch); preg_match('/(.*)/i', $contents, $title); $title = str_replace(array(" ", " ", " ", ',', ' '), '', $title[1]); return $title; } $t_url = "https://example.com"; // 初始化变量 $file_get_contents_title = ""; $curl_title = ""; // 创建两个线程,分别使用file_get_contents和curl获取网站标题 $file_get_contents_thread = new Thread(function() use($t_url, &$file_get_contents_title) { $file_get_contents_title = getTitle($t_url); }); $curl_thread = new Thread(function() use($t_url, &$curl_title) { $curl_title = getTitleCurl($t_url); }); // 启动线程 $file_get_contents_thread->start(); $curl_thread->start(); // 等待两个线程执行完毕 $file_get_contents_thread->join(); $curl_thread->join(); // 检查哪个线程先获取到网站标题,并使用该标题 if ($file_get_contents_title !== "") { $title = $file_get_contents_title; } else { $title = $curl_title; } echo $title; ?> PHP获取网站标题:最快速度获取目标网站标题的两种方法比较

原文地址: https://www.cveoy.top/t/topic/o7Rx 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录