원격 작업 함수 정보

6725 단어 함수
/**
* Enhanced file_get_contents, use curl to fetch remote file
* @param $file
* @author Wilson Zeng
*/
function ex_file_get_contents($file){
//if is url?
if(preg_match('/^[a-zA-z]+:\/\/[^\s]*/i', $file)){//a url
$ch = curl_init($file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$rs = curl_exec($ch);
curl_close($ch);
return $rs;
}else{
return file_get_contents($file);
}
}

/**
* Enhanced copying file, can copy remote file(use curl)
* @param $url
* @param $destpath
* @author Wilson Zeng
*/
function trans_file($url, $destpath){
if(preg_match('/^[a-zA-z]+:\/\/[^\s]*/i', $url)){//a url
$ch = curl_init($url);
$opts = array(
CURLOPT_RETURNTRANSFER => 1,
);
curl_setopt_array($ch, $opts);
$file_str = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($http_code < 200 || $http_code >= 300 || file_put_contents($destpath, $file_str) <= 0){
return FALSE;
}
}else{//a local file
return copy($url, $destpath);
}
return TRUE;
}

/**
* POST/GET remote url
* @author Wilson Zeng
* @param $url
* @param $timeout [optinal]
* @param $post_data [optional]
* @param $http_code [optional] return http code
* @return RESPONSE
*/
function get_remote_file($url, $timeout = 0, $post_data = NULL, &$http_code = NULL, &$curl_errno = NULL, &$curl_error = NULL){
$ch = curl_init($url);
$opts[CURLOPT_RETURNTRANSFER] = 1;
if($post_data){
$opts[CURLOPT_POST] = 1;
if(is_array($post_data)){
$opts[CURLOPT_POSTFIELDS] = http_build_query($post_data);
}elseif(is_string($post_data)){
$opts[CURLOPT_POSTFIELDS] = $post_data;
}
}
if($timeout > 0){
$opts[CURLOPT_TIMEOUT] = $timeout;
}
curl_setopt_array($ch, $opts);
$rs = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);
return $rs;
}

좋은 웹페이지 즐겨찾기