很多站长朋友们都不太清楚php如何获取网页元素,今天小编就来给大家整理php如何获取网页元素,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php如何像asp.net一样直接获取页面元素的值 2、 用PHP如何获取网页审查元素中的内容? 3、 php获取指定网页内容 4、 PHP 如何获取到一个网页的内容 php如何像asp.net一样直接获取页面元素的值1、PHP4以后获取传值的方法。
2、旧版本PHP3以前的获取传值方法。
3、修改PHP.ini文件的配置。
用PHP如何获取网页审查元素中的内容?使用file_get_contents可以获取网页源码
<?php
$homepage = file_get_contents ( '' );
echo $homepage ;
?>
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 如何获取到一个网页的内容1.file_get_contents
PHP代码
复制代码 代码如下:
<?php
$url = "";
$contents = file_get_contents($url);
//如果出现中文乱码使用下面代码
//$getcontent = iconv("gb2312", "utf-8",$contents);
echo $contents;
?>
2.curl
PHP代码
复制代码 代码如下:
<?php
$url = "";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
//在需要用户检测的网页里需要增加下面两行
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
//curl_setopt($ch, CURLOPT_USERPWD, US_NAME.":".US_PWD);
$contents = curl_exec($ch);
curl_close($ch);
echo $contents;
?>
3.fopen->fread->fclose
PHP代码
复制代码 代码如下:
<?php
$handle = fopen ("", "rb");
$contents = "";
do {
$data = fread($handle, 1024);
if (strlen($data) == 0) {
break;
}
$contents .= $data;
} while(true);
fclose ($handle);
echo $contents;
?>
注:
1.
使用file_get_contents和fopen必须空间开启allow_url_fopen。方法:编辑php.ini,设置
allow_url_fopen = On,allow_url_fopen关闭时fopen和file_get_contents都不能打开远程文件。
2.使用curl必须空间开启curl。方法:windows下修改php.ini,将extension=php_curl.dll前面的分
号去掉,而且需要拷贝ssleay32.dll和libeay32.dll到C:\WINDOWS\system32下;Linux下要安装curl扩
展。
关于php如何获取网页元素的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php如何获取网页元素 php获取网站html代码的详细内容...