php获取指定网页内容

2024年11月20日 19:40
有5个网友回答
网友(1):

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

$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方式获取内容

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

$html= file_get_contents($url);

echo$html;

?>

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

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

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

while(!feof($fp)) {

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

}

echo"url header: {$header}
":

echo"url body: $result";

fclose($fp);

?>

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

$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扩展

$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;

?>

网友(2):

此类方法一共有三种

  1. 第一种方法

$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>/i",$data, $title);</p> <p>echo $title[1];</p> <p>?></p> <p><img loading="lazy" class="ikqb_img" src="/picurl?url=https%3A%2F%2Fiknow-pic.cdn.bcebos.com%2Fadaf2edda3cc7cd91745d0f03701213fb80e91ab%3Fx-bce-process%3Dimage%252Fresize%252Cm_lfit%252Cw_600%252Ch_800%252Climit_1%252Fquality%252Cq_85%252Fformat%252Cf_auto" esrc="https://iknow-pic.cdn.bcebos.com/adaf2edda3cc7cd91745d0f03701213fb80e91ab"></p> <p>第二种方法:使用file()函数</p> <p><?php</p> <p>$lines_array = file('http://www.badcatxt.com/');</p> <p>$lines_string = implode('', $lines_array); </p> <p>$pos = strpos($lines_string,'utf-8');</p> <p>if($pos===false){$lines_string = iconv("gbk","utf-8",$lines_string);} </p> <p>eregi("<title>(.*)", $lines_string, $title);

echo $title[1];

?>

第三种方法:使用file_get_contents

$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,'')+7;</p> <p>$poste=strpos($content,'');

$length=$poste-$postb;

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

?>

网友(3):

用正则表达式,是最快的,你看下面:

$url = 'http://www.baidu.com'; //这儿填页面地址
$info=file_get_contents($url);
preg_match('|(.*?)<\/title>|i',$info,$m);<br>echo $m[1];<br>?></p> </div> </div> <div class="clear"></div> </div> <div class="wdhdnr"> <div class="huidanrtop"> <div class="wdhuidaxinx"> <div class="wdhuidaxm">网友(4):</div> </div> </div> <div class="clear"></div> <div class="wdhuidanrmid"> <div class="zuijiacont"> <p>是这个 preg_match('/<title>(.*?)<\/title>/i',$info,$m);</p> </div> </div> <div class="clear"></div> </div> <div class="wdhdnr"> <div class="huidanrtop"> <div class="wdhuidaxinx"> <div class="wdhuidaxm">网友(5):</div> </div> </div> <div class="clear"></div> <div class="wdhuidanrmid"> <div class="zuijiacont"> <p>用正则表达式,是最快的,你看下面:<br><?php<br>$url<br>=<br>'http://www.baidu.com';<br>//这儿填页面地址<br>$info=file_get_contents($url);<br>preg_match('|<title>(.*?)<\/title>|i',$info,$m);<br>echo<br>$m[1];<br>?></p> </div> </div> <div class="clear"></div> </div> </div> </div> <div class="wendaright"> <div class="wdluluerwema"> <div class="wdxgwttop">相关问答</div> <div class="wdxgwtnr"> </div> <div class="clear"></div> </div> <!-- 其他随机问答['id'=>alphaID($like['zid'])] --> <div class="wdluluerwema"> <div class="wdxgwttop">最新问答</div> <div class="wdxgwtnr"> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://czqahb.com/article/129713128.html">想入手一款2500左右的家用相机!要拍照和摄像都差不多的!请专家推荐一下</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://czqahb.com/article/412017417.html">电脑开不开机光驱一直响</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://czqahb.com/article/878463206770021012.html">正常血压是多少范围我的血压是高压93</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://czqahb.com/article/1053162982542162339.html">怎样克服自己的狂妄自大心理?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://czqahb.com/article/813225580171600372.html">gba三国志孔明传孔明自立结局如何出现?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://czqahb.com/article/1448802373186160980.html">北京几环限号,怎么限的,我是外地车跟本就搞不懂</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://czqahb.com/article/445858910.html">要一套QQ个性签名 网名 头像 空间呢称 个人说明 全都要有一点伤感的,女生的</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://czqahb.com/article/2053991866679451347.html">单招报考学校官网出了拟录取名单是不是就等于录取了</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://czqahb.com/article/389290585.html">我在广州,寄快递到香港,哪家快递香港邮政小包最便宜</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://czqahb.com/article/1305602592265329059.html">想配一台电脑,要求玩lol特效全开fps稳定在120左右。问下这样的电脑最低配置是什么?要具体点的</a></div> </div> </div> </div> </div> <div class="clear"></div> <div class="footer"> <!-- 移动底部导航 --> <div class="fanhuitop"><a href="#top" ref="nofollow"><img src="https://czqahb.com/static/old/img/fhtop.png" title="返回顶部"></a></div> <div class="dibu"> <div class="dibu"> </div> </div> <div class="banquan"> <p>内容全部来源于网络收集,如有侵权,请联系网站删除:QQ:24596024</p> </div> </div> </div> </div> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?0c9f72217b0fd07e33b2b148bdd4490c"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>