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;
?>