php获取指定网页内容

比如代码<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xht... 比如代码<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>无标题文档</title>
</head>

<body>
</body>
</html>
我想让PHP只获取到title中间的标题也就是只获取“无标题文档”的字样, 请问代码怎么写?
展开
 我来答
大野瘦子
高粉答主

2018-12-15 · 繁杂信息太多,你要学会辨别
知道小有建树答主
回答量:1227
采纳率:100%
帮助的人:31.7万
展开全部

一、用file_get_contents函数,以post方式获取url

<?php

$url= 'http://www.domain.com/test.php?id=123';

$data= array('foo'=> 'bar');

$data= http_build_query($data); 

$opts= array(

'http'=> array(

'method'=> 'POST',

'header'=>"Content-type: application/x-www-form-urlencoded\r\n"  .

"Content-Length: "  . strlen($data) . "\r\n",

'content'=> $data

)

);

$ctx= stream_context_create($opts);

$html= @file_get_contents($url,'',$ctx);

二、用file_get_contents以get方式获取内容

<?php

$url='http://www.domain.com/?para=123';

$html= file_get_contents($url);

echo$html;

?>

三、用fopen打开url, 以get方式获取内容

<?php

$fp= fopen($url,'r');

$header= stream_get_meta_data($fp);//获取报头信息

while(!feof($fp)) {

$result.= fgets($fp, 1024);

}

echo"url header: {$header} <br>":

echo"url body: $result";

fclose($fp);

?>

四、用fopen打开url, 以post方式获取内容

<?php

$data= array('foo2'=> 'bar2','foo3'=>'bar3');

$data= http_build_query($data);

$opts= array(

'http'=> array(

'method'=> 'POST',

'header'=>"Content-type: application/x-www-form-

urlencoded\r\nCookie:cook1=c3;cook2=c4\r\n"  .

"Content-Length: "  . strlen($data) . "\r\n",

'content'=> $data

)

); 

$context= stream_context_create($opts);

$html= fopen('http://www.test.com/zzzz.php?id=i3&id2=i4','rb',false, $context);

$w=fread($html,1024);

echo$w;

?>

五、使用curl库,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展

<?php

$ch= curl_init();

$timeout= 5;

curl_setopt ($ch, CURLOPT_URL, 'http://www.domain.com/');

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

$file_contents= curl_exec($ch);

curl_close($ch);

echo$file_contents;

?>

网络情的海角
2019-03-04 · TA获得超过178个赞
知道答主
回答量:4
采纳率:0%
帮助的人:4774
展开全部

此类方法一共有三种

  1. 第一种方法

<?php

$c = curl_init();

$url = 'www.badcatxt.com'; 

curl_setopt($c, CURLOPT_URL, $url); 

curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);

$data = curl_exec($c);
curl_close($c);

$pos = strpos($data,'utf-8');

if($pos===false){$data = iconv("gbk","utf-8",$data);} 

preg_match("/<title>(.*)<\/title>/i",$data, $title);

echo $title[1];

?>

第二种方法:使用file()函数

<?php

$lines_array = file('http://www.badcatxt.com/');

$lines_string = implode('', $lines_array); 

$pos = strpos($lines_string,'utf-8');

if($pos===false){$lines_string = iconv("gbk","utf-8",$lines_string);} 

eregi("<title>(.*)</title>", $lines_string, $title);

echo $title[1];

?>

第三种方法:使用file_get_contents

<?php 

$content=file_get_contents("http://www.badcatxt.com/");

$pos = strpos($content,'utf-8');

if($pos===false){$content = iconv("gbk","utf-8",$content);}

$postb=strpos($content,'<title>')+7;

$poste=strpos($content,'</title>');

$length=$poste-$postb;

echo substr($content,$postb,$length);

?>

本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
我手我口
推荐于2017-11-28 · TA获得超过807个赞
知道小有建树答主
回答量:565
采纳率:61%
帮助的人:312万
展开全部
用正则表达式,是最快的,你看下面:

<?php
$url = 'http://www.baidu.com'; //这儿填页面地址
$info=file_get_contents($url);
preg_match('|<title>(.*?)<\/title>|i',$info,$m);
echo $m[1];
?>
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
dawnco
2010-03-07 · TA获得超过324个赞
知道小有建树答主
回答量:789
采纳率:0%
帮助的人:401万
展开全部
是这个 preg_match('/<title>(.*?)<\/title>/i',$info,$m);
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
阮彦尚昊天
2019-05-29 · TA获得超过3898个赞
知道大有可为答主
回答量:3133
采纳率:31%
帮助的人:246万
展开全部
用正则表达式,是最快的,你看下面:
<?php
$url
=
'http://www.baidu.com';
//这儿填页面地址
$info=file_get_contents($url);
preg_match('|<title>(.*?)<\/title>|i',$info,$m);
echo
$m[1];
?>
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(4)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式