首页 > 编程知识 正文

php获取内容,php获取数据库内容

时间:2023-12-27 22:28:04 阅读:327237 作者:WRZL

本文目录一览:

php获取指定网页内容

一、用file_get_contents函数,以post方式获取url

?php

$url= '';

$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

)

);

$ctx= stream_context_create($opts);

$html= @file_get_contents($url,'',$ctx);

二、用file_get_contents以get方式获取内容

?php

$url='';

$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-

urlencodedrnCookie:cook1=c3;cook2=c4rn"  .

"Content-Length: "  . strlen($data) . "rn",

'content'= $data

)

);

$context= stream_context_create($opts);

$html= fopen(';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, '');

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

$file_contents= curl_exec($ch);

curl_close($ch);

echo$file_contents;

?

PHP怎么获取里面的内容

1、用file_get_contents,以get方式获取内容。

2、用fopen打开url,以get方式获取内容。

3、使用curl库,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展。

4、用file_get_contents函数,以post方式获取url。

php如何读取文本指定的内容?

php读取文件内容:

-----第一种方法-----fread()--------

?php

$file_path = "test.txt";

if(file_exists($file_path)){

$fp = fopen($file_path,"r");

$str = fread($fp,filesize($file_path));//指定读取大小,这里把整个文件内容读取出来

echo $str = str_replace("rn","br /",$str);

}

?

--------第二种方法------------

?php

$file_path = "test.txt";

if(file_exists($file_path)){

$str = file_get_contents($file_path);//将整个文件内容读入到一个字符串中

$str = str_replace("rn","br /",$str);

echo $str;

}

?

-----第三种方法------------

?php

$file_path = "test.txt";

if(file_exists($file_path)){

$fp = fopen($file_path,"r");

$str = "";

$buffer = 1024;//每次读取 1024 字节

while(!feof($fp)){//循环读取,直至读取完整个文件

$str .= fread($fp,$buffer);

}

$str = str_replace("rn","br /",$str);

echo $str;

}

?

-------第四种方法--------------

?php

$file_path = "test.txt";

if(file_exists($file_path)){

$file_arr = file($file_path);

for($i=0;$icount($file_arr);$i++){//逐行读取文件内容

echo $file_arr[$i]."br /";

}

/*

foreach($file_arr as $value){

echo $value."br /";

}*/

}

?

----第五种方法--------------------

?php

$file_path = "test.txt";

if(file_exists($file_path)){

$fp = fopen($file_path,"r");

$str ="";

while(!feof($fp)){

$str .= fgets($fp);//逐行读取。如果fgets不写length参数,默认是读取1k。

}

$str = str_replace("rn","br /",$str);

echo $str;

}

?

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。