php获取指定网页内容

2024-11-20 19:40:49
推荐回答(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://58z.net/z/2140198960511172948.html">大众迈腾中控台图解使用方法</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://58z.net/z/1446066899599206140.html">真的有命相学吗?换句话说 人的命是注定的吗?算命的真的是准的吗?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://58z.net/z/1448241183887329460.html">开除党籍处分应降多少工资 开除公职后的退休待遇如何</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://58z.net/z/2123637595310645427.html">哪种流产伤害小恢复快?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://58z.net/z/431826421334779524.html">我想迁户口都需要什么手续</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://58z.net/z/617469891224359412.html">公募基金有没有倒闭的可能性,若公募基金倒闭,会不会影响公募基金专户理财发行的私募基金产品</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://58z.net/z/1453167681103780260.html">照相馆灯光装修设计要点有哪些?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://58z.net/z/655796623913355085.html">美容师具体都做啥?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://58z.net/z/1890014358489919228.html">全自动老试洗衣机只能脱水不能洗涤衣服怎么办?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://58z.net/z/2078151826786030028.html">深圳北站到深圳体育馆怎么走</a></div> </div> </div> </div> </div> <div class="clear"></div> <div class="footer"> <!-- 移动底部导航 --> <div class="fanhuitop"><a href="#top" ref="nofollow"><img src="https://58z.net/static/old/img/fhtop.png" alt="返回顶部" 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?bc215519b6d2b72984cdb0a467b5dcf6"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>