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;
    }
  • 相关阅读:
    00-03.kaliLinux-vi粘贴复制功能配置
    00-02.kaliLinux-配置SSH服务
    00-01.Kali Linux 2020.1修改root用户密码
    Web设计色彩(转载)
    Inno Setup区段之Language篇
    Inno Setup区段之Setup篇
    IE无法获取到input框的值
    Inno Setup之常量篇
    Inno Setup之概念篇
    网络不通排查
  • 原文地址:https://www.cnblogs.com/ppoo24/p/2324535.html
Copyright © 2011-2022 走看看