php获取指定网页内容

php获取指定网页内容,第1张

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

<php

$url= '>

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

$data= >

$opts= array(

'>

'method'=> 'POST',

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

"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='>

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

$opts= array(

'>

'method'=> 'POST',

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

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

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

'content'=> $data

)

);

$context= stream_context_create($opts);

$html= fopen('>

$w=fread($html,1024);

echo$w;

>

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

<php

$ch= curl_init();

$timeout= 5;

curl_setopt ($ch, CURLOPT_URL, '>

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

$file_contents= curl_exec($ch);

curl_close($ch);

echo$file_contents;

>

$url = '你的远程地址';

$html = file_get_contents($url);

//根据需求填写正则表达式

preg_match_all("/<div>([^<]+)<\/div>/", $string, $matches);

//打印出你要的内容

print_r($matches);

以上就是关于php获取指定网页内容全部的内容,包括:php获取指定网页内容、php怎么获取远程页面中的一个div的内容、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/9763569.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-01
下一篇 2023-05-01

发表评论

登录后才能评论

评论列表(0条)

保存