使用php怎么对文件的内容进行读取

本篇文章为大家展示了使用php怎么对文件的内容进行读取,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

示例代码1: 用file_get_contents 以get方式获取内容

代码如下:


<?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方式获取内容

代码如下:


<?
$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函数,以post方式获取url

代码如下:


<?php
$data=https://www.yisu.com/zixun/array('foo'=>'bar');
$data=https://www.yisu.com/zixun/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函数打开url,以get方式获取完整的数据,包括header和body

代码如下:


<?
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=https://www.yisu.com/zixun/get_url($url,$cookie);
if($rowdata)
{
 $body=stristr($rowdata,"\r\n\r\n");
 $body=substr($body,4,strlen($body));
 return$body;
}
returnfalse;
}
?>


示例代码5:用fsockopen函数打开url,以POST方式获取完整的数据,包括header和body

代码如下:


<?
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. 引用
引用美元=?11“;
//使字符串从数据
foreach (dataas关键=美元在美元值)
$值[]=?关键=?urlencode(美元值);
$ data_string=内爆(“,“美元值),
//找出哪些端口是必需的,如果没有使用标准(=80)
如果(!收取($ URL_Info [“port"]))
URL_Info美元[“port"]=80;
//建筑POST请求:
$请求。=胺⒉肌啊C涝猆RL_Info (“path") !”HTTP/1.1 \ n";
$请求。=爸鞒秩?“。美元URL_Info干净自己的[“host"] \ n";
$请求。=巴萍鋈?推荐人\ n"美元;;
$请求。=澳谌堇嘈?应用程序/x-www-form-urlencoded \ n";
$请求。=澳谌莩ざ?“.strlen (data_string美元)干净\ n";
$请求。=傲?紧密\ n";
$请求。=氨?饼干\ n"美元;;
$请求。=癨 n";
请求。美元=$ data_string干净\ n";
$ fp=fsockopen (URL_Info美元[“host"],美元URL_Info [“port"]);
fputs (fp,请求美元);
, (! feof (fp)美元){
$结果。=fgets (fp 1024美元);
}
文件关闭(fp);
返回结果美元;
}
printhr ();
?在


示例代码6:使用curl库,使用curl库之前,你可能需要查看一下php。ini,查看是否已经打开了旋度扩展

代码如下:


& lt; ?
$ ch=curl_init ();
超时=5美元;
curl_setopt ($ ch CURLOPT_URL & # 39; http://www.baidu.com/& # 39;);
curl_setopt (ch美元CURLOPT_RETURNTRANSFER 1);
curl_setopt ($ ch CURLOPT_CONNECTTIMEOUT超时美元);
$ file_contents=curl_exec (ch);
curl_close (ch);
echo $ file_contents;
?在


关于curl库:
卷官方网站http://curl.haxx。se/
旋度是使用URL语法的传送文件工具,支持FTP, FTP, HTTP HTPPS SCP SFTP TFTP TELNET DICT文件和LDAP.curl支持SSL证书,HTTP POST, HTTP, FTP上传,kerberos,基于计画格式的上传,代理,饼干,用户+口令证明,文件传送恢复,HTTP代理通道和大量其他有用的技巧

使用php怎么对文件的内容进行读取