php获取网页
functiongetRemoteRes($url,$postfields=NULL,$timeout=60){
$ci=curl_init();
curl_setopt($ci,CURLOPT_URL,$url);
curl_setopt($ci,CURLOPT_HEADER,FALSE);
curl_setopt($ci,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ci,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ci,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ci,CURLOPT_TIMEOUT,$timeout);
curl_setopt($ci,CURLOPT_POST,TRUE);
if(is_array($postfields)){
$field_str="";
foreach($postfieldsas$k=>$v){
$field_str.="&$k=".urlencode($v);
}
curl_setopt($ci,CURLOPT_POSTFIELDS,$field_str);
}
$response=curl_exec($ci);
if(curl_errno($ci)){
return'ERRNO!';
}else{
$httpStatusCode=curl_getinfo($ci,CURLINFO_HTTP_CODE);
if(200!==$httpStatusCode){
return'ERRNO!';
}
}
curl_close($ci);
return$response;
}
先用以上函数获取指定的网页,然后从返回的数据中解析出你要的数据.可以使用正则表达式来提取,这要根据你要获取的页面源代码来判断了.暂时未知,以上只是提供一个思路给你.
② php从url获取网页内容
$url="http://..com/question/123456789.html";
$content=file_get_contents($url);
$replace="abc";
preg_replace("/<div>/Ui",$replace,$content,);
楼上几来位说的都对,我的例子自替换全部<div>标签,具体的那个,你要看前后的特征信息在匹配。
③ php获取网页源码内容有哪些办法
1、使用file_get_contents获得网页源代码。这个方法最常用,只需要两行代码即可,非常简单方便。
2、使用fopen获得网页源代码。这个方法用的人也不少,不过代码有点多。
3、使用curl获得网页源代码。使用curl获得网页源代码的做法,往往是需要更高要求的人使用,例如当你需要在抓取网页内容的同时,得到网页header信息,还有ENCODING编码的使,USERAGENT的使用等等。
④ php获取指定网页内容
一、用_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如何抓取网页中的数据
<divid="Div3"class="modResumeInfo">
<divclass="title"onclick="clickLabel(rsmEExCt)">
<divclass="dcrLdcrArrowGreen"></div>
<h3>外语能力</h3>
</div>
<divid="Div4"class="content">
<divclass="workExCom">英语:回读写答能力精通|听说能力熟练</div>
<divclass="workExCom">韩语:读写能力一般|听说能力良好</div>
<divclass="workExCom">德语:读写能力一般|听说能力一般</div>
</div>
</div><!--modResumeInfo结束-->
⑥ php抓取页面内容
|
<?php
$rs=file_get_contents('http://www.boc.cn/sourcedb/whpj/enindex.html');
preg_match('/<tablewidth="600"border="0"cellpadding="5"cellspacing="1"bgcolor="#EAEAEA">(.*?)</table>/sS',$rs,$match);
//print_r($match);
$rs=str_replace(array('</tr>','</td>','<tralign="center">','<tdbgcolor="#FFFFFF">'),array('|',';'),$match[1]);
//www.hi-docs.com/php/str_replace.html
$data=array();
$rs=explode('|',$rs);
foreach($rsas$key=>$item){
if($key>0){
$arr=explode(';',$item);
($a=@trim($arr[0]))&&($b=@trim($arr[5]))&&$data[]=array($a,$b);
}
}
print_r($data);
?>
⑦ 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获取网页部分数据
用正则表达式,是最快的,你看下面:
<?php
$url = 'http://www..com'; //这儿专填页面地址属
$info=file_get_contents($url);
preg_match('|<title>(.*?)<\/title>|i',$info,$m);
echo $m[1];
?>
⑨ php如何获得调用网页的网址
|
用file()函数
下面的代码是
通过PHP的File函数库来完成上传图像文件并让其显示
//FILE1:
<?php
//fulldirectorypath
$filepath="/home/httpd/html/tut/upload";
//200Kisthemaximum(picture)filesizetobeaccepted
define("MAX_FILE_SIZE",200*1024);
functionprint_error($err){
echo"<h1>$err</h1><hr>";
}
do{
//;ifnot,skiptothe
//"while(false)"sectionof"do"statement
if(isset($picture)){
//
//doesn'texceedmaximumallowablesize
if(getenv("CONTENT_LENGTH")>MAX_FILE_SIZE){
print_error("Filetoolarge:$picture_name");
break;
}
//;"@"prefixtellsfopennottoprint
//messageifthereisanerror,sincefunctionprint_errordoesthat
//ifthereisanerror,breakoutof"do"loopandcontinueat"while(false)"
$fp=@fopen($picture,"r");
if(!$fp){
print_error("Cannotopenfile:$picture_name");
break;
}
//generateuniquenameforsession,useittogenerateuniqueserver
//directoryname,andcreatethedirectory
srand((double)microtime()*1000000);
$id=md5(uniqid(rand()));
$dirname="$filepath/$id";
mkdir($dirname,0700);
//
$filename=$dirname."/picture";
//;"@"prefixtellsfopennotto
//printmessageifthereisanerror,sincefunctionprint_errordoesthat
//ifthereisanerror,breakoutof"do"loopandcontinueat"while(false)"
$out=@fopen($filename,"w");
if(!$out){
print_error("Cannotopenfile:$filename");
break;
}
//
while($buffer=fread($fp,8192)){
fwrite($out,$buffer);
}
//
fclose($fp);
fclose($out);
//;thisfilewillholdthe
//nameofthepicturefile
$filename=$dirname."/name";
//;"@"prefixtellsfopennottoprint
//messageifthereisanerror,sincefunctionprint_errordoesthat
//ifthereisanerror,breakoutof"do"loopandcontinueat"while(false)"
$out=@fopen($filename,"w");
if(!$out){
print_error("Cannotopenfile:$filename");
break;
}
//,andclosetheserver
//namefile
fputs($out,$name);
fclose($out);
//
//server,,andsupply
//theHTMLlink
?>
Pictureadded.Thanks.<br>
<ahref="upload_display.php">Continuetothegallery</a>
<?php
//exittotheserverphotogallery
exit();
}
}while(false);
//yougettohereonlywhen"if(isset($picture))"isfalse,whichmeansthat
//nopicturenamehasbeensubmitted,
//
?>
<!--startuploadform-->
<!DOCTYPEHTMLPUBLIC"-//IETF//DTDHTML//EN">
<html>
<head>
<title>Photogallery-add</title>
</head>
<bodybgcolor="white">
<h1>Photogalleryadd</h1>
<?php
//
//using$PHP_SELFforvalueof"formaction"causesformtorefertoitself
//when"submit"buttonisclicked
?>
<formaction="<?echo$PHP_SELF?>"method=POSTENCTYPE="multipart/form-data">
<?php
//passthePHPconstantMAX_FILE_SIZEtotheHTMLmaximumfilesize
//constantMAX_FILE_SIZE
?>
<INPUTTYPE="hidden"name="MAX_FILE_SIZE"value="<?echoMAX_FILE_SIZE?>">
<?php
//,andstore
//;browsingisenabled
?>
Yournameis:<INPUTNAME="name"><br>
Yourpicture:<INPUTNAME="picture"TYPE="file"><br>
<?php
//displaythe"submit"button
?>
<INPUTTYPE="submit"VALUE="Addpicture"name="send">
</form>
</body>
</html>
//------------------------------------------------------
//FILE2:DISPLAYTHESERVERPHOTOGALLERY
<!DOCTYPEHTMLPUBLIC"-//IETF//DTDHTML//EN">
<html>
<head>
<title>Photogallery</title>
</head>
<body>
<h1>Photogallery</h1>
<?php
//fulldirectorypath
$filepath="/home/httpd/html/tut/upload";
//user'spathinbrowser--sameasfulldirectorypath
$url_path="/tut/upload";
//
$dir=dir($filepath);
//
while($entry=$dir->read()){
//ifentryissystemfile(doesn'thavepicturefiles),gotonextentryin
//"while"loop
if($entry=="."||$entry==".."){
continue;
}
//openservernamefileforreadonly;"@"prefixtellsfopennottoprint
//messageifthereisanerror,sincefunctionprint_errordoesthat
//ifthereisanerror,gotonextentryin"while"loop
$fp=@fopen("$filepath/$entry/name","r");
if(!$fp){
print"Badentry:$entry<br>";
continue;
}
//
$name=fgets($fp,4096);
fclose($fp);
//;inaddition,"alt="causesthefile
//
?>
<imgsrc="<?echo"$url_path/$entry/picture"?>"
alt="<?echo$name?>"><b><?echo$name?></b><br>
<?
}
?>
</body>
</html>
⑩ php获得当前网页的名称
感觉ls方法不好,用basename函数结合PHP_SELF便可获取当前文件的文件名,这也是discuz的写法
<?php
$curfile = basename($_SERVER['PHP_SELF']);
echo $curfile;
?>