首页 > php代码 > php 模拟用户获取远程文件内容

php 模拟用户获取远程文件内容

1,ini_get : Returns the value of the configuration option as a string on success, or an empty string on failure(读取 php教程.ini 配置文件中的值) 

2,; Whether to allow the treatment of URLs (like http:// or ftp://) as files. 

allow_url_fopen = On(配置文件中的内容) 

3,fopen( "rb"): 在操作二进制文件时如果没有指定 'b' 标记,可能会碰到一些奇怪的问题,包括坏掉的图片文件以及关于 rn 字符的奇怪问题。 

注意: 为移植性考虑,强烈建议在用 fopen() 打开文件时总是使用 'b' 标记。 

注意: 再一次,为移植性考虑,强烈建议你重写那些依赖于 't' 模式的代码使其使用正确的行结束符并改成 'b' 模式。 

4,strtolower -- Make a string lowercase 

5,curl_init() :curl_init -- Initialize a cURL session(初始化一个cUrl会话)


<?php
/** 
 获取远程文件内容
 @param $url 文件http地址
 */
function fopen_url($url) {
    if (function_exists('file_get_contents')) {
        $file_content = @file_get_contents($url);
    } elseif (ini_get('allow_url_fopen') && ($file = @fopen($url, 'rb'))) {
        $i = 0;
        while (!feof($file) && $i++ < 1000) {
            $file_content.= strtolower(fread($file, 4096));
        }
        fclose($file);
    } elseif (function_exists('curl_init')) {
        $curl_handle = curl_init();
        curl_setopt($curl_handle, CURLOPT_URL, $url);
        curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
        curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl_handle, CURLOPT_FAILONERROR, 1);
        curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Trackback Spam Check'); //引用垃圾邮件检查
        $file_content = curl_exec($curl_handle);
        curl_close($curl_handle);
    } else {
        $file_content = '';
    }
    return $file_content;
}


永久链接:http://www.phprm.com/code/6b8cc129ec48828e16043ae717f8dc7c.html

转载随意!带上文章地址吧。

标签:none

发表留言