php获得网页数据 php获取网页数据

47-网页获取数据的方法(get-post)通过网页表单获取的数据 , 在php文件中呈现 , 利用php方法中的$_GET方法接受 , 提交的数据为一个字典 。
1、通过输入网址请求服务器中的html文件 , 服务器接受请求文件,进行处理
2、服务器接收后,处理成响应报文进行返回到用户浏览器界面
3、第二次在html的表单中提交的数据会形成请求报文到服务器中,php文件接受数据并进行处理
4、服务器中php文件接收后会处理并返回响应文件呈现到用户浏览器界面
将form表单中的method的取值改成post就是以post的方式将文件放给服务器 。
1、相同点
2、不同点
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 = https://www.04ip.com/post/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下获取网页内容的几种方法:
用file_get_contents,以get方式获取内容 。
用fopen打开url,以get方式获取内容 。
使用curl库php获得网页数据,使用curl库之前php获得网页数据 , 可能需要查看一下php.ini是否已经打开php获得网页数据了curl扩展 。
用file_get_contents函数,以post方式获取url 。
用fopen打开url,以post方式获取内容 。
用fsockopen函数打开url,获取完整的数据,包括header和body 。
php获取指定网页内容一、用file_get_contents函数,以post方式获取url
?php
$url= '';
$data= https://www.04ip.com/post/array('foo'= 'bar');
$data= https://www.04ip.com/post/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);//获取报头信息

推荐阅读