zoukankan      html  css  js  c++  java
  • 关于远程操作的函数

    /**
    * 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;
    }
  • 相关阅读:
    Educational Codeforces Round 86 (Rated for Div. 2)
    第十六届东南大学大学生程序设计竞赛(春、夏季)
    Codeforces Round #643 (Div. 2)
    [P3384] 【模板】轻重链剖分
    [BJOI2012] 连连看
    [CF1349C] Orac and Game of Life
    Codeforces Round #641 (Div. 2)
    [TJOI2018] 数学计算
    [CF1157D] N Problems During K Days
    [CF1163C1] Power Transmission (Easy Edition)
  • 原文地址:https://www.cnblogs.com/ppoo24/p/2324535.html
Copyright © 2011-2022 走看看