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://116p.com/p/2208213560148717428.html">《绝地求生大逃杀》新手用什么武器比较好 最强</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://116p.com/p/627035272389310524.html">公募基金和私募基金有什么区别</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://116p.com/p/746470748010114732.html">女生们:如果你对一个男生非常有好感,他也非常喜欢你,这时你会怎么办?会让他发现你也喜欢他吗?你会有</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://116p.com/p/1447271410513418580.html">求函数y=arctan1-x눀⼀1+x눀的导数</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://116p.com/p/1306558544047831179.html">WPS文字怎么快速换行</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://116p.com/p/154278014.html">家乡美景的作文400字</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://116p.com/p/1861039981209038547.html">剑网三扶摇11重任务有刷出来的吗?我纯阳的。。。</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://116p.com/p/558505269586461372.html">李白和苏轼:谁是古代文学第一人</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://116p.com/p/585347933.html">数学和应用数学专业的就业前景?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://116p.com/p/1835359095179295300.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://116p.com/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?47623f177e8ec8769ce50576dee7a63c"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>