這篇文章主要介紹了關于php獲取遠程文件內容的函數代碼,使用這個函數也可以獲取圖片
一個簡單的php獲取遠程文件內容的函數代碼,兼容性強。直接調用就可以輕松獲取遠程文件的內容,使用這個函數也可獲取圖片。代碼如下:
- /**
- * 讀遠程內容
- * @return string
- */
- function get_url_content($url){
- if(function_exists("curl_init")){
- $ch = curl_init();
- $timeout = 30;
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
- $file_contents = curl_exec($ch);
- curl_close($ch);
- }else{
- $is_auf=ini_get('allow_url_fopen')?true:false;
- if($is_auf){
- $file_contents = file_get_contents($url);
- }
- }
- return $file_contents;
- }
以上就是php獲取遠程文件內容的函數代碼,希望這篇文章對大家學習php程序設計有所幫助。
新聞熱點
疑難解答