资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

php调用网页数据 php调用html

php获取指定网页内容

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

目前创新互联公司已为上千余家的企业提供了网站建设、域名、虚拟主机、网站托管、服务器租用、企业网站设计、鹤山网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

?php

$url= '';

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

$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(';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, '');

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

$file_contents= curl_exec($ch);

curl_close($ch);

echo$file_contents;

?

PHP怎么调用网页

a.php

?php

/*不知道你是想跳转还是取回网页内容后显示

$type为TRUE时采用页面跳转方式

$type为FALSE时采用取回内容后显示

*/

$type = TRUE;

if( isset( $_GET['url'] ) ){

$url = 'http://' . $_GET['url'];

if( $type ){

header("Location: $url");

}else{

$page = file_get_contents($url);

echo $page;

}

}else{

echo '未设置URL参数';

}

?

用PHP获取网页部分数据

如果你要

之间的所有源码,用 preg_match 就可以,不用preg_match_all ,如果你要里面的所有的

标签中的内容,可以用preg_match_all //提取所有代码 $pattern = '/

(.+?)

/is'; preg_match($pattern, $string, $match); //$match[0] 即为

之间的所有源码 echo $match[0]; //然后再提取

之间的内容 $pattern = '/(.+?)li/is'; preg_match_all($pattern, $match[0], $results); $new_arr=array_unique($results[0]); foreach($new_arr as $kkk){ echo $kkk; }


分享标题:php调用网页数据 php调用html
浏览地址:http://cdkjz.cn/article/dogdhso.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220