PHP 使用 获取网页内容:
if (! preg_match('@^https?://@', $url)) { die('HTTP(s) only.'); } $httpOpts = array( 'max_redirects' => 0 ,'timeout' => 3 ); if ($ua !== '') { $httpOpts['user_agent'] = $ua; } if ($cook !== '') { $httpOpts['header'] = "Cookie: {$cook}\r\n"; } $ctx = stream_context_create(array( 'http' => $httpOpts )); // fetch if (! ($fp = @fopen($url, 'r', false, $ctx))) { die('Couldn\'t open URL.'); } $meta = stream_get_meta_data($fp); $content = stream_get_contents($fp); fclose($fp);
本文介绍了一种使用PHP从指定URL抓取网页内容的方法。通过设置HTTP选项,如超时时间和重定向次数,可以更好地控制请求过程。此外,还可以自定义User-Agent和Cookie头来模拟不同的浏览器行为。
278

被折叠的 条评论
为什么被折叠?



