当前位置:Gxlcms > PHP教程 > PHP使用stream_get_meta_date,stream_get_contents获取网页内容

PHP使用stream_get_meta_date,stream_get_contents获取网页内容

时间:2021-07-01 10:21:17 帮助过:3人阅读

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));// fetchif (! ($fp = @fopen($url, 'r', false, $ctx))) {    die('Couldn\'t open URL.');}$meta = stream_get_meta_data($fp);$content = stream_get_contents($fp);fclose($fp);

人气教程排行