PHP 獲取遠程文件內(nèi)容的函數(shù)代碼
更新時間:2010年03月24日 19:38:03 作者:
PHP 獲取遠程文件內(nèi)容的代碼,后面有一些注釋可以參考下,其實大家可以參考腳本之家發(fā)布的一些采集程序代碼。
如下函數(shù):
<?
/**
獲取遠程文件內(nèi)容
@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;
}
?>
相關(guān)解釋:
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(配置文件中的內(nèi)容)
3,fopen( "rb"): 在操作二進制文件時如果沒有指定 'b' 標(biāo)記,可能會碰到一些奇怪的問題,包括壞掉的圖片文件以及關(guān)于 \r\n 字符的奇怪問題。
注意: 為移植性考慮,強烈建議在用 fopen() 打開文件時總是使用 'b' 標(biāo)記。
注意: 再一次,為移植性考慮,強烈建議你重寫那些依賴于 't' 模式的代碼使其使用正確的行結(jié)束符并改成 'b' 模式。
4,strtolower -- Make a string lowercase
5,curl_init() :curl_init -- Initialize a cURL session(初始化一個cUrl會話)
resource curl_init ( [string url] )
Initializes a new session and return a cURL handle for use with the curl_setopt(), curl_exec(), and curl_close() functions.
url--If provided, the CURLOPT_URL option will be set to its value. You can manually set this using the curl_setopt() function.
Returns a cURL handle on success, FALSE on errors.
6,curl_setopt -- Set an option for a cURL transfer(提供設(shè)置)
bool curl_setopt ( resource ch, int option, mixed value )
Sets an option on the given cURL session handle. (具體請看 PHP 手冊) There:
CURLOPT_URL :The URL to fetch. You can also set this when initializing a session with curl_init().
CURLOPT_CONNECTTIMEOUT :The number of seconds to wait whilst trying to connect. Use 0 to wait indefinitely.(無限期等待 設(shè)置為 0)
CURLOPT_RETURNTRANSFER :TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
CURLOPT_FAILONERROR :TRUE to fail silently if the HTTP code returned is greater than or equal to 400. The default behavior is to return the page normally, ignoring the code.
CURLOPT_USERAGENT :The contents of the "User-Agent: " header to be used in a HTTP request.
7,curl_exec : Perform a cURL session, This function should be called after you initialize a cURL session and all the options for the session are set.
如果成功則返回 TRUE,失敗則返回 FALSE。 However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure
8,curl_close -- Close a cURL session
下面是一些參考代碼:
PHP 采集程序 常用函數(shù)
PHP 采集獲取指定網(wǎng)址的內(nèi)容
復(fù)制代碼 代碼如下:
<?
/**
獲取遠程文件內(nèi)容
@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;
}
?>
相關(guān)解釋:
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(配置文件中的內(nèi)容)
3,fopen( "rb"): 在操作二進制文件時如果沒有指定 'b' 標(biāo)記,可能會碰到一些奇怪的問題,包括壞掉的圖片文件以及關(guān)于 \r\n 字符的奇怪問題。
注意: 為移植性考慮,強烈建議在用 fopen() 打開文件時總是使用 'b' 標(biāo)記。
注意: 再一次,為移植性考慮,強烈建議你重寫那些依賴于 't' 模式的代碼使其使用正確的行結(jié)束符并改成 'b' 模式。
4,strtolower -- Make a string lowercase
5,curl_init() :curl_init -- Initialize a cURL session(初始化一個cUrl會話)
resource curl_init ( [string url] )
Initializes a new session and return a cURL handle for use with the curl_setopt(), curl_exec(), and curl_close() functions.
url--If provided, the CURLOPT_URL option will be set to its value. You can manually set this using the curl_setopt() function.
Returns a cURL handle on success, FALSE on errors.
6,curl_setopt -- Set an option for a cURL transfer(提供設(shè)置)
bool curl_setopt ( resource ch, int option, mixed value )
Sets an option on the given cURL session handle. (具體請看 PHP 手冊) There:
CURLOPT_URL :The URL to fetch. You can also set this when initializing a session with curl_init().
CURLOPT_CONNECTTIMEOUT :The number of seconds to wait whilst trying to connect. Use 0 to wait indefinitely.(無限期等待 設(shè)置為 0)
CURLOPT_RETURNTRANSFER :TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
CURLOPT_FAILONERROR :TRUE to fail silently if the HTTP code returned is greater than or equal to 400. The default behavior is to return the page normally, ignoring the code.
CURLOPT_USERAGENT :The contents of the "User-Agent: " header to be used in a HTTP request.
7,curl_exec : Perform a cURL session, This function should be called after you initialize a cURL session and all the options for the session are set.
如果成功則返回 TRUE,失敗則返回 FALSE。 However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure
8,curl_close -- Close a cURL session
下面是一些參考代碼:
PHP 采集程序 常用函數(shù)
PHP 采集獲取指定網(wǎng)址的內(nèi)容
您可能感興趣的文章:
相關(guān)文章
PHP實現(xiàn)機器學(xué)習(xí)之樸素貝葉斯算法詳解
這篇文章主要介紹了PHP實現(xiàn)機器學(xué)習(xí)之樸素貝葉斯算法,結(jié)合實例形式詳細(xì)分析了樸素貝葉斯算法的概念、原理及php實現(xiàn)技巧,需要的朋友可以參考下2017-12-12php解析xml提示Invalid byte 1 of 1-byte UTF-8 sequence錯誤的處理方法
在利用php解析xml時提示Invalid byte 1 of 1-byte UTF-8 sequence錯誤了,這個問題我百度查實說是編碼問題,結(jié)果我把編碼處理一下果然KO了,下面我來分享一下解決辦法2013-11-11基于php數(shù)組中的索引數(shù)組和關(guān)聯(lián)數(shù)組詳解
下面小編就為大家分享一篇基于php數(shù)組中的索引數(shù)組和關(guān)聯(lián)數(shù)組詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03PHP數(shù)組編碼gbk與utf8互相轉(zhuǎn)換的兩種方法
這篇文章給大家分享了兩種轉(zhuǎn)換的方法,都可以實現(xiàn)PHP數(shù)組的編碼進行相互轉(zhuǎn)換。大家可以參考自己的實際情況進行選擇哪一種方法,下面來一起看看吧。2016-09-09PHP基于單例模式實現(xiàn)的數(shù)據(jù)庫操作基類
這篇文章主要介紹了PHP基于單例模式實現(xiàn)的數(shù)據(jù)庫操作基類,涉及PHP操作數(shù)據(jù)庫的基本配置與增刪改查等操作技巧,需要的朋友可以參考下2016-01-01