天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

php讀取文件內(nèi)容的幾種方法詳解

示例代碼1: 用file_get_contents 以get方式獲取內(nèi)容
復(fù)制代碼 代碼如下:
<?php
$url='http://www.baidu.com/';
$html=file_get_contents($url);
//print_r($http_response_header);
ec($html);
printhr();
printarr($http_response_header);
printhr();
?>

示例代碼2: 用fopen打開url, 以get方式獲取內(nèi)容
復(fù)制代碼 代碼如下:
<?
$fp=fopen($url,'r');
printarr(stream_get_meta_data($fp));
printhr();
while(!feof($fp)){
$result.=fgets($fp,1024);
}
echo"url body:$result";
printhr();
fclose($fp);
?>

示例代碼3:用file_get_contents函數(shù),以post方式獲取url
復(fù)制代碼 代碼如下:
<?php
$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
),
);
$context=stream_context_create($opts);
$html=file_get_contents('http://localhost/e/admin/test.html',false,$context);
echo$html;
?>

示例代碼4:用fsockopen函數(shù)打開url,以get方式獲取完整的數(shù)據(jù),包括header和body
復(fù)制代碼 代碼如下:
<?
functionget_url($url,$cookie=false){
$url=parse_url($url);
$query=$url[path]."?".$url[query];
ec("Query:".$query);
$fp=fsockopen($url[host],$url[port]?$url[port]:80,$errno,$errstr,30);
if(!$fp){
returnfalse;
}else{
$request="GET$queryHTTP/1.1/r/n";
$request.="Host:$url[host]/r/n";
$request.="Connection: Close/r/n";
if($cookie)$request.="Cookie:$cookie/n";
$request.="/r/n";
fwrite($fp,$request);
while(!@feof($fp)){
$result.=@fgets($fp,1024);
}
fclose($fp);
return$result;
}
}
//獲取url的html部分,去掉header
functionGetUrlHTML($url,$cookie=false){
$rowdata=get_url($url,$cookie);
if($rowdata)
{
 $body=stristr($rowdata,"/r/n/r/n");
 $body=substr($body,4,strlen($body));
 return$body;
}
returnfalse;
}
?>

示例代碼5:用fsockopen函數(shù)打開url,以POST方式獲取完整的數(shù)據(jù),包括header和body
復(fù)制代碼 代碼如下:
<?
functionHTTP_Post($URL,$data,$cookie,$referrer=""){
// parsing the given URL
$URL_Info=parse_url($URL);
// Building referrer
if($referrer=="")// if not given use this script. as referrer
$referrer="111";
// making string from $data
foreach($dataas$key=>$value)
$values[]="$key=".urlencode($value);
$data_string=implode("&",$values);
// Find out which port is needed - if not given use standard (=80)
if(!isset($URL_Info["port"]))
$URL_Info["port"]=80;
// building POST-request:
$request.="POST ".$URL_Info["path"]." HTTP/1.1/n";
$request.="Host: ".$URL_Info["host"]."/n";
$request.="Referer:$referer/n";
$request.="Content-type: application/x-www-form-urlencoded/n";
$request.="Content-length: ".strlen($data_string)."/n";
$request.="Connection: close/n";
$request.="Cookie:$cookie/n";
$request.="/n";
$request.=$data_string."/n";
$fp=fsockopen($URL_Info["host"],$URL_Info["port"]);
fputs($fp,$request);
while(!feof($fp)){
$result.=fgets($fp,1024);
}
fclose($fp);
return$result;
}
printhr();
?>

示例代碼6:使用curl庫,使用curl庫之前,你可能需要查看一下php.ini,查看是否已經(jīng)打開了curl擴(kuò)展
復(fù)制代碼 代碼如下:
<?
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, 'http://www.baidu.com/');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?>

關(guān)于curl庫:
curl官方網(wǎng)站http://curl.haxx.se/
curl 是使用URL語法的傳送文件工具,支持FTP、FTPS、HTTP HTPPS SCP SFTP TFTP TELNET DICT FILE和LDAP。curl 支持SSL證書、HTTP POST、HTTP PUT 、FTP 上傳,kerberos、基于HTT格式的上傳、代理、cookie、用戶+口令證明、文件傳送恢復(fù)、http代理通道和大量其他有用的技巧
復(fù)制代碼 代碼如下:
<?
functionprintarr(array$arr)
{
echo"<br> Row field count: ".count($arr)."<br>";
foreach($arras$key=>$value)
{
echo"$key=$value <br>";
}
}
?>

======================================================
php抓取遠(yuǎn)程網(wǎng)站數(shù)據(jù)的代碼
現(xiàn)在可能還有很多程序愛好者都會遇到同樣的疑問,就是要如何像搜索引擎那樣去抓取別人網(wǎng)站的HTML代碼,然后把代碼收集整理成為自己有用的數(shù)據(jù)!今天就等我介紹一些簡單例子吧.
Ⅰ.抓取遠(yuǎn)程網(wǎng)頁標(biāo)題的例子:
以下是代碼片段:
復(fù)制代碼 代碼如下:
<?php
/*
+-------------------------------------------------------------
+抓取網(wǎng)頁標(biāo)題的代碼,直接拷貝本代碼片段,另存為.php文件執(zhí)行即可.
+-------------------------------------------------------------
*/
error_reporting(7);
$file = fopen ("http://www.dnsing.com/", "r");
if (!$file) {
echo "<font color=red>Unable to open remote file.</font>/n";
exit;
}
while (!feof ($file)) {
$line = fgets ($file, 1024);
if (eregi ("<title>(.*)</title>", $line, $out)) {
$title = $out[1];
echo "".$title."";
break;
}
}
fclose($file);
//End
?>

Ⅱ.抓取遠(yuǎn)程網(wǎng)頁HTML代碼的例子:
以下是代碼片段:
復(fù)制代碼 代碼如下:
<? php
/*
+----------------
+DNSing Sprider
+----------------
*/
$fp = fsockopen("www.dnsing.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br/>/n";
} else {
$out = "GET / HTTP/1.1/r/n";
$out .= "Host:www.dnsing.com/r/n";
$out .= "Connection: Close /r/n/r/n";
fputs($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
//End
?>

以上兩個代碼片段都直接Copy回去運(yùn)行就知道效果了,上面的例子只是抓取網(wǎng)頁數(shù)據(jù)的雛形,要使其更適合自己的使用,情況有各異.所以,在此各位程序愛好者自己好好研究一下吧.
===============================
稍微有點(diǎn)意義的函數(shù)是:get_content_by_socket(), get_url(), get_content_url(), get_content_object 幾個函數(shù),也許能夠給你點(diǎn)什么想法。
復(fù)制代碼 代碼如下:
<?php
//獲取所有內(nèi)容url保存到文件
function get_index($save_file, $prefix="index_"){
$count = 68;
$i = 1;
if (file_exists($save_file)) @unlink($save_file);
$fp = fopen($save_file, "a+") or die("Open ". $save_file ." failed");
while($i<$count){
$url = $prefix . $i .".htm";
echo "Get ". $url ."...";
$url_str = get_content_url(get_url($url));
echo " OK/n";
fwrite($fp, $url_str);
++$i;
}
fclose($fp);
}
//獲取目標(biāo)多媒體對象
function get_object($url_file, $save_file, $split="|--:**:--|"){
if (!file_exists($url_file)) die($url_file ." not exist");
$file_arr = file($url_file);
if (!is_array($file_arr) || empty($file_arr)) die($url_file ." not content");
$url_arr = array_unique($file_arr);
if (file_exists($save_file)) @unlink($save_file);
$fp = fopen($save_file, "a+") or die("Open save file ". $save_file ." failed");
foreach($url_arr as $url){
if (empty($url)) continue;
echo "Get ". $url ."...";
$html_str = get_url($url);
echo $html_str;
echo $url;
exit;
$obj_str = get_content_object($html_str);
echo " OK/n";
fwrite($fp, $obj_str);
}
fclose($fp);
}
//遍歷目錄獲取文件內(nèi)容
function get_dir($save_file, $dir){
$dp = opendir($dir);
if (file_exists($save_file)) @unlink($save_file);
$fp = fopen($save_file, "a+") or die("Open save file ". $save_file ." failed");
while(($file = readdir($dp)) != false){
if ($file!="." && $file!=".."){
 echo "Read file ". $file ."...";
 $file_content = file_get_contents($dir . $file);
 $obj_str = get_content_object($file_content);
 echo " OK/n";
 fwrite($fp, $obj_str);
}
}
fclose($fp);
}
//獲取指定url內(nèi)容
function get_url($url){
$reg = '/^http:////[^//].+$/';
if (!preg_match($reg, $url)) die($url ." invalid");
$fp = fopen($url, "r") or die("Open url: ". $url ." failed.");
while($fc = fread($fp, 8192)){
$content .= $fc;
}
fclose($fp);
if (empty($content)){
die("Get url: ". $url ." content failed.");
}
return $content;
}
//使用socket獲取指定網(wǎng)頁
function get_content_by_socket($url, $host){
$fp = fsockopen($host, 80) or die("Open ". $url ." failed");
$header = "GET /".$url ." HTTP/1.1/r/n";
$header .= "Accept: */*/r/n";
$header .= "Accept-Language: zh-cn/r/n";
$header .= "Accept-Encoding: gzip, deflate/r/n";
$header .= "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; InfoPath.1; .NET CLR 2.0.50727)/r/n";
$header .= "Host: ". $host ."/r/n";
$header .= "Connection: Keep-Alive/r/n";
//$header .= "Cookie: cnzz02=2; rtime=1; ltime=1148456424859; cnzz_eid=56601755-/r/n/r/n";
$header .= "Connection: Close/r/n/r/n";
fwrite($fp, $header);
while (!feof($fp)) {
$contents .= fgets($fp, 8192);
}
fclose($fp);
return $contents;
}
//獲取指定內(nèi)容里的url
function get_content_url($host_url, $file_contents){
//$reg = '/^(#|Javascript.*?|ftp:////.+|http:////.+|.*?href.*?|play.*?|index.*?|.*?ASP)+$/i';
//$reg = '/^(down.*?/.html|/d+_/d+/.htm.*?)$/i';
$rex = "/([hH][rR][eE][Ff])/s*=/s*['/"]*([^>'/"/s]+)[/"'>]*/s*/i";
$reg = '/^(down.*?/.html)$/i';
preg_match_all ($rex, $file_contents, $r);
$result = ""; //array();
foreach($r as $c){
if (is_array($c)){
 foreach($c as $d){
if (preg_match($reg, $d)){ $result .= $host_url . $d."/n"; }
 }
}
}
return $result;
}
//獲取指定內(nèi)容中的多媒體文件
function get_content_object($str, $split="|--:**:--|"){
$regx = "/href/s*=/s*['/"]*([^>'/"/s]+)[/"'>]*/s*(<b>.*?<//b>)/i";
preg_match_all($regx, $str, $result);
if (count($result) == 3){
$result[2] = str_replace("<b>多媒體: ", "", $result[2]);
$result[2] = str_replace("</b>", "", $result[2]);
$result = $result[1][0] . $split .$result[2][0] . "/n";
}
return $result;
}
?>

======================================================
同一域名對應(yīng)多個IP時,php獲取遠(yuǎn)程網(wǎng)頁內(nèi)容的函數(shù)
fgc就是簡單的讀取過來,把一切操作封裝了
fopen也進(jìn)行了一些封裝,但是需要你循環(huán)讀取得到所有數(shù)據(jù)。
fsockopen這是直板板的socket操作。
如果僅僅是讀取一個html頁面,fgc更好。
如果公司是通過防火墻上網(wǎng),一 般的file_get_content函數(shù)就不行了。當(dāng)然,通過一些socket操作,直接向proxy寫http請求也是可以的,但是比較麻煩。
如果你能確認(rèn)文件很小,可以任選以上兩種方式fopen ,join('',file($file));。比如,你只操作小于1k的文件,那最好還是用file_get_contents吧。
如果確定文件很大,或者不能確定文件的大小,那就最好使用文件流了。fopen一個1K的文件和fopen一個1G的文件沒什么明顯的區(qū)別。內(nèi)容長,就可以花更長的時間去讀,而不是讓腳本死掉。
----------------------------------------------------
http://www.phpcake.cn/archives/tag/fsockopen
php獲取遠(yuǎn)程網(wǎng)頁內(nèi)容有多種方式,例如用自帶的file_get_contents、fopen等函數(shù)。
復(fù)制代碼 代碼如下:
<?php
echo file_get_contents("http://blog.s135.com/abc.php");
?>

但是,在DNS輪詢等負(fù)載均衡中,同一域名,可能對應(yīng)多臺服務(wù)器,多個IP。假設(shè)blog.s135.com被DNS解析到 72.249.146.213、72.249.146.214、72.249.146.215三個IP,用戶每次訪問blog.s135.com,系統(tǒng)會根據(jù)負(fù)載均衡的相應(yīng)算法訪問其中的一臺服務(wù)器
上周做一個視頻項(xiàng)目時,就碰到這樣一類需求:需要依次訪問每臺服務(wù)器上的一個php接口程序(假設(shè)為abc.php),查詢這臺服務(wù)器的傳輸狀態(tài)。
這時就不能直接用file_get_contents訪問http://blog.s135.com/abc.php了,因?yàn)樗赡芤恢敝貜?fù)訪問某一臺服務(wù)器
而采用依次訪問http://72.249.146.213/abc.php、http://72.249.146.214/abc.php、http://72.249.146.215/abc.php的方法,在這三臺服務(wù)器上的Web Server配有多個虛擬主機(jī)時,也是不行的。
通過設(shè)置本地hosts也不行,因?yàn)閔osts不能設(shè)置多個IP對應(yīng)同一個域名。
那就只有通過php和HTTP協(xié)議來實(shí)現(xiàn):訪問abc.php時,在header頭中加上blog.s135.com域名。于是,我寫了下面這個php函數(shù):
復(fù)制代碼 代碼如下:
<?php
 /************************
 * 函數(shù)用途:同一域名對應(yīng)多個IP時,獲取指定服務(wù)器的遠(yuǎn)程網(wǎng)頁內(nèi)容
 * 參數(shù)說明:
 * $ip服務(wù)器的IP地址
 * $host服務(wù)器的host名稱
 * $url服務(wù)器的URL地址(不含域名)
 * 返回值:
 * 獲取到的遠(yuǎn)程網(wǎng)頁內(nèi)容
 * false訪問遠(yuǎn)程網(wǎng)頁失敗
 ************************/
 function HttpVisit($ip, $host, $url)
 {
$errstr = '';
$errno = '';
$fp = fsockopen ($ip, 80, $errno, $errstr, 90);
if (!$fp)
{
 return false;
}
else
{
$out = "GET {$url} HTTP/1.1/r/n";
$out .= "Host:{$host}/r/n";
$out .= "Connection: close/r/n/r/n";
fputs ($fp, $out);

while($line = fread($fp, 4096)){
$response .= $line;
}
fclose( $fp );

//去掉Header頭信息
$pos = strpos($response, "/r/n/r/n");
$response = substr($response, $pos + 4);

return $response;
}
 }

 //調(diào)用方法:
 $server_info1 = HttpVisit("72.249.146.213", "blog.s135.com", "/abc.php");
 $server_info2 = HttpVisit("72.249.146.214", "blog.s135.com", "/abc.php");
 $server_info3 = HttpVisit("72.249.146.215", "blog.s135.com", "/abc.php");
 ?>

php技術(shù)php讀取文件內(nèi)容的幾種方法詳解,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 最新高清无码专区在线视频 | 99热这里只有精品8 99热这里只有精品6 | 第一次处破女高清电影 | 无码天堂亚洲国产AV久久 | 久久久黄色片 | 国产成人久久AV免费看澳门 | 在线 中文字幕 | 成人国产精品日本在线 | 妓女嫖客叫床粗话对白 | 把英语老师强奷到舒服动态图 | 国产探花在线精品一区二区 | 日本高清不卡码无码v亚洲 日本福利片午夜免费观着 日本粉嫩学生毛绒绒 | 影音先锋av电影 | 欧美高清videosgratis高 | 国产视频成人 | 亚洲综合无码一区二区 | 久草在在线免视频在线观看 | 免费看到湿的小黄文软件APP | 亚洲精品自在线拍2019 | 伊人久在线观看视频 | 亚洲欧美日韩综合影院 | 一个人的HD高清在线观看 | 草草久久久无码国产专区全集观看 | 国产精品一区二区AV白丝在线 | 妖精视频免费高清观看 | 99国产热视频在线观看 | 国产成人高清亚洲一区app | 亚洲AV久久无码精品国产网站 | 天美传媒在线观看免费完整版 | 真实国产熟睡乱子伦对白无套 | 99久久久A片无码国产精 | metart中国撒尿人体欣赏 | 2020最新国产自产精品 | 国产亚洲tv在线观看 | 国产成人在线网站 | 国语对白刺激真实精品 | 寂寞夜晚免费观看视频 | 色多多污污版免费下载安装 | 欧美5g影院天天爽天天看 | 俺来也俺去也视频久久 | 伊人久久亚洲综合天堂 |