PHP file_get_contents函数简析
时间:2014-07-16 20:49 来源: 我爱IT技术网 作者:山风
file_get_contents主要是用来取得文件内容的,不同于files的地方是 file_get_contents() 是把文件读写成一个字符串。
函式的参数如下,path是一定要给的,context 抓取格式有限定时做处理的。

- // file_get_contents(path,include_path,context,start,max_length)
- $file_content=file_get_contents("google.txt",FILE_USE_INCLUDE_PATH,NULL,0, 10);
- echo $file_content;
- //抓取网页资讯
- $URL="http://www.yahoo.com.tw";
- $opts = array(
- 'http'=>array(
- 'method'=>"GET",
- 'header'=>"Accept-language: en\r\n" .
- "Cookie: foo=bar\r\n"
- )
- );
- $context = stream_context_create($opts);
- $file = file_get_contents($URL, false, $context);
- echo $file;
效果展示:

其他狀況:
PHP执行中中,如果抓取https的网站,会出现错误提示:
- Warning: file_get_contents(https://localhost/myphp/php_files.php)
- [function.file-get-contents]: failed to open stream: Invalid argument in I:\Web\myphp\a.php on line 12
此时,打开php.ini文件,搜 ;extension=php_openssl.dll ,去掉分号;,然后重启ssl服务,重启web服务器即可,Apache也可以同時启用mod_ssl模块测试。
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
