新聞中心
怎么用php讀取并顯示另一個php文件的內容?
示例代碼1: 用file_get_contents 以get方式獲取內容

成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網綜合服務,包含不限于網站設計、做網站、平湖網絡推廣、小程序制作、平湖網絡營銷、平湖企業(yè)策劃、平湖品牌公關、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們最大的嘉獎;成都創(chuàng)新互聯(lián)公司為所有大學生創(chuàng)業(yè)者提供平湖建站搭建服務,24小時服務熱線:028-86922220,官方網址:www.cdcxhl.com
代碼如下:
?php
$url='';
$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函數(shù),以post方式獲取url
代碼如下:
?php
$data=array('foo'='bar');
$data=http_build_query($data);
$opts=array(
'http'=array(
'method'='POST',
'header'="Content-type: application/x-www-form-urlencodedrn".
"Content-Length: ".strlen($data)."rn",
'content'=$data
),
);
$context=stream_context_create($opts);
$html=file_get_contents('',false,$context);
echo$html;
?
示例代碼4:用fsockopen函數(shù)打開url,以get方式獲取完整的數(shù)據(jù),包括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.1rn";
$request.="Host:$url[host]rn";
$request.="Connection: Closern";
if($cookie)$request.="Cookie:$cookien";
$request.="rn";
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,"rnrn");
$body=substr($body,4,strlen($body));
return$body;
}
returnfalse;
}
?
示例代碼5:用fsockopen函數(shù)打開url,以POST方式獲取完整的數(shù)據(jù),包括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. 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.1n";
$request.="Host: ".$URL_Info["host"]."n";
$request.="Referer:$referern";
$request.="Content-type: application/x-www-form-urlencodedn";
$request.="Content-length: ".strlen($data_string)."n";
$request.="Connection: closen";
$request.="Cookie:$cookien";
$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,查看是否已經打開了curl擴展
代碼如下:
?
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, '');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?
關于curl庫:
curl官方網站
curl 是使用URL語法的傳送文件工具,支持FTP、FTPS、HTTP HTPPS SCP SFTP TFTP TELNET DICT FILE和LDAP。curl 支持SSL證書、HTTP POST、HTTP PUT 、FTP 上傳,kerberos、基于HTT格式的上傳、代理、cookie、用戶+口令證明、文件傳送恢復、http代理通道和大量其他有用的技巧
復制代碼 代碼如下:
?
functionprintarr(array$arr)
{
echo"br Row field count: ".count($arr)."br";
foreach($arras$key=$value)
{
echo"$key=$value br";
}
}
?
php 怎么POST獲取數(shù)據(jù)?
方法1、最常見的方法是:$_POST['fieldname'];
說明:只能接收Content-Type:
application/x-www-form-urlencoded提交的數(shù)據(jù)
解釋:也就是表單POST過來的數(shù)據(jù)
方法2、file_get_contents("php://input");
說明:
允許讀取
POST
的
原始數(shù)據(jù)
。
和
$HTTP_RAW_POST_DATA
比起來,它給內存帶來的壓力較小,并且不需要任何特殊的
php.ini
設置。
php://input
不能用于
enctype="multipart/form-data"。
解釋:
對于未指定
Content-Type
的POST數(shù)據(jù),則可以使用file_get_contents(“php://input”);來獲取原始數(shù)據(jù)。
事實上,用PHP接收POST的任何數(shù)據(jù)都可以使用本方法。而不用考慮Content-Type,包括
二進制文件
流也可以。
所以用方法二是最保險的方法
方法3、$GLOBALS['HTTP_RAW_POST_DATA'];
說明:
總是產生
$HTTP_RAW_POST_DATA
變量包含有原始的
POST
數(shù)據(jù)。
此變量僅在碰到未識別
MIME
類型的數(shù)據(jù)時產生。
$HTTP_RAW_POST_DATA
對于
enctype="multipart/form-data"
表單數(shù)據(jù)不可用
如果post過來的數(shù)據(jù)不是PHP能夠識別的,可以用
$GLOBALS['HTTP_RAW_POST_DATA']來接收,
比如
text/xml
或者
soap
等等
解釋:
$GLOBALS['HTTP_RAW_POST_DATA']存放的是POST過來的原始數(shù)據(jù)。
$_POST或
$_REQUEST
存放的是
PHP以key=value的形式格式化以后的數(shù)據(jù)。
但$GLOBALS['HTTP_RAW_POST_DATA']中是否保存POST過來的數(shù)據(jù)取決于centent-Type的設置,即POST數(shù)據(jù)時
必須顯式示指明Content-Type:
application/x-www-form-urlencoded,POST的數(shù)據(jù)才會存放到
$GLOBALS['HTTP_RAW_POST_DATA']中
如何正確理解PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)
1、PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)之 mysql_result()
mixed mysql_result(resource result_set, int row [,mixed field])
從result_set 的指定row 中獲取一個field 的數(shù)據(jù). 簡單但是效率低.
舉例:
$link1 = @mysql_connect("server1",
"webuser", "password")
or die("Could not connect
to mysql server!");
@mysql_select_db("company")
or die("Could not select database!");
$query = "select id, name
from product order by name";
$result = mysql_query($query);
$id = mysql_result($result, 0, "id");
$name = mysql_result($result, 0, "name");
mysql_close();
注意,上述代碼只是輸出結果集中的第一條數(shù)據(jù)的字段值,如果要輸出所有記錄,需要循環(huán)處理.
for ($i = 0; $i = mysql_num_rows($result); $i++)
{
$id = mysql_result($result, 0, "id");
$name = mysql_result($result, 0, "name");
echo "Product: $name ($id)";
}
注意,如果查詢字段名是別名,則mysql_result中就使用別名.
2、PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)之mysql_fetch_row()
array mysql_fetch_row(resource result_set)
從result_set中獲取整行,把數(shù)據(jù)放入數(shù)組中.
舉例(注意和list 的巧妙配合):
$query = "select id,
name from product order by name";
$result = mysql_query($query);
while(list($id, $name)
= mysql_fetch_row($result)) {
echo "Product: $name ($id)";
}
3、PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)之mysql_fetch_array()
array mysql_fetch_array(resource result_set [,int result_type])
mysql_fetch_row()的增強版.
將result_set的每一行獲取為一個關聯(lián)數(shù)組或/和數(shù)值索引數(shù)組.
默認獲取兩種數(shù)組,result_type可以設置:
MYSQL_ASSOC:返回關聯(lián)數(shù)組,字段名=字段值
MYSQL_NUM:返回數(shù)值索引數(shù)組.
MYSQL_BOTH:獲取兩種數(shù)組.因此每個字段可以按索引偏移引用,也可以按字段名引用.
舉例:
$query = "select id,
name from product order by name";
$result = mysql_query($query);
while($row = mysql_fetch_array
($result, MYSQL_BOTH)) {
$name = $row['name'];
//或者 $name = $row[1];
$name = $row['id'];
//或者 $name = $row[0];
echo "Product: $name ($id)";
}
4、PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)之mysql_fetch_assoc()
array mysql_fetch_assoc(resource result_set)
相當于 mysql_fetch_array($result, MYSQL_ASSOC)
5、PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)之mysql_fetch_object()
object mysql_fetch_object(resource result_set)
和mysql_fetch_array()功能一樣,不過返回的不是數(shù)組,而是一個對象.
舉例:
$query = "select id, name
from product order by name";
$result = mysql_query($query);
while($row = mysql_fetch_object
($result)) {
$name = $row-name;
$name = $row-id;
echo "Product: $name ($id)";
}
以上這些函數(shù)就是PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)的全部總結。
新聞名稱:php獲取php數(shù)據(jù) php獲取系統(tǒng)信息
鏈接分享:http://www.dlmjj.cn/article/hisidd.html


咨詢
建站咨詢
