可以参考以下几种方法:
方法一: file_get_contents获取
<span style="white-space:pre"></span>$url="http://www..com/";
<span style="white-space:pre"></span>$fh= file_get_contents
('http://www.hxfzzx.com/news/fzfj/');<span style="white-space:pre"></span>echo $fh;
拓展资料
PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,利于学习,使用广泛,主要适用于Web开发领域。PHP 独特的语法混合了C、Java、Perl以及PHP自创的语法。它可以比CGI或者Perl更快速地执行动态网页。
用PHP做出的动态页面与其他的编程语言相比,PHP是将程序嵌入到HTML(标准通用标记语言下的一个应用)文档中去执行,执行效率比完全生成HTML标记的CGI要高许多;PHP还可以执行编译后代码,编译可以达到加密和优化代码运行,使代码运行更快。
❷ php怎么抓取其它网站数据
可以用以下4个方法来抓取网站 的数据:
1. 用 file_get_contents 以 get 方式获取内容:
?
$url = 'http://localhost/test2.php';
$html = file_get_contents($url);
echo $html;
2. 用fopen打开url,以get方式获取内容
?
$url = 'http://localhost/test2.php';
$fp = fopen($url, 'r');
stream_get_meta_data($fp);
$result = '';
while(!feof($fp))
{
$result .= fgets($fp, 1024);
}
echo "url body: $result";
fclose($fp);
3. 用file_get_contents函数,以post方式获取url
?
$data = array(
'foo'=>'bar',
'baz'=>'boom',
'site'=>'www.jb51.net',
'name'=>'nowa magic');
$data = http_build_query($data);
//$postdata = http_build_query($data);
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $data
//'timeout' => 60 * 60 // 超时时间(单位:s)
)
);
$url = "http://localhost/test2.php";
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;
4、使用curl库,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展
$url = 'http://localhost/test2.php?site=jb51.net';
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $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 = "http://www.jb51.net";
$contents = file_get_contents($url);
//如果出现中文乱码使用下面代码
//$getcontent = iconv("gb2312", "utf-8",$contents);
echo $contents;
?>
2.curl
PHP代码
复制代码 代码如下:
<?php
$url = "http://www.jb51.net";
$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 ("http://www.jb51.net", "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怎么调用网页
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如何获取当前页面url路径
#测试网址: http://localhost/blog/testurl.php?id=5
//获取域名或主机地址
echo $_server['http_host']."
"; #localhost
//获取网页地址
echo $_server['php_self']."
"; #/blog/testurl.php
//获取网址参数
echo $_server["query_string"]."
"; #id=5
//获取用户代理
echo $_server['http_referer']."
";
//获取完整的url
echo 'http://'.$_server['http_host'].$_server['request_uri'];
echo 'http://'.$_server['http_host'].$_server['php_self'].'?'.$_server['query_string'];
#http://localhost/blog/testurl.php?id=5
//包含端口号的完整url
echo 'http://'.$_server['server_name'].':'.$_server["server_port"].$_server["request_uri"];
#http://localhost:80/blog/testurl.php?id=5
//只取路径
$url='http://'.$_server['server_name'].$_server["request_uri"];
echo dirname($url);
#http://localhost/blog
❻ 如何通过php获取提交页面的URL
在PHP的开发中我们经常会通过网址URL向另一个网页传递参数的问题。在这个过程中我们首先需要获取到当前页面的URL,然后将URL中各个参数的值保存到变量中。整个过程较为简单,主要涉及到$_SERVER的用法。
1、$_server['http_host'],作用:获取网址域名,如(www.5ibobo.com,这是波波的一个博客,暂且做例子吧)。
2、$_SERVER["PHP_SELF"],作用:获取网页地址,如(/code/445.html)。
3、$_SERVER["QUERY_STRING"],作用:获取网址URL参数,待会我们会在实例中用到。
4、$_SERVER["HTTP_REFERER"],作用:获取用户的代理。
❼ php抓取网页源码方法
可以使用file_get_content函数来获取源代码,你只需要把网站传入这个函数,获取后是一个字符串,你需要格式化代码就可以了
❽ PHP中怎样发送post请求并获取网页
$post='POST数据';
//初始化
$curl=curl_init('URL');
$header=array();
$header[]='User-Agent:Mozilla/5.0(WindowsNT6.1)AppleWebKit/537.36(KHTML,likeGecko)Chrome/42.0.2311.90Safari/537.36';
curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
//不输出header头信息
curl_setopt($curl,CURLOPT_HEADER,0);
//保存到字符串而不是输出
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
//post数据
curl_setopt($curl,CURLOPT_POST,1);
//请求数据
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
//是否抓取跳转后的页面
curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1);
$response=curl_exec($curl);
curl_close($curl);
echo$response;
❾ php获取指定网页内容
一、用file_get_contents函数,以post方式获取url
<?php
$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 " .
"Content-Length: " .strlen($data) ." ",
'content'=>$data
)
);
$ctx= stream_context_create($opts);
$html= @file_get_contents($url,'',$ctx);
二、用file_get_contents以get方式获取内容
<?php
$url='http://www.domain.com/?para=123';
$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 Cookie:cook1=c3;cook2=c4 " .
"Content-Length: " .strlen($data) ." ",
'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扩展
<?php
$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;
?>
❿ PHP获取网页内容的几种方法
简单的收集下PHP下获取网页内容的几种方法:
用file_get_contents,以get方式获取内容。
用fopen打开url,以get方式获取内容。
使用curl库,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展。
用file_get_contents函数,以post方式获取url。
用fopen打开url,以post方式获取内容。
用fsockopen函数打开url,获取完整的数据,包括header和body。