curl实用远程传输工具

349 查看

/**

  • curl远程传输工具
    */
    public function postCurl_new($url,$body,$header,$type='POST'){
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_HEADER,0);//0只要正文,1全部信息
    curl_setopt($ch,CURLOPT_TIMEOUT,5);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    // array_push($header,'Accept:application/json');
    // array_push($header,'Content-Type:application/json');
    // array_push($header,'http:multipart/form-data');
    if(count($body)>0){
    curl_setopt($ch,CURLOPT_POSTFIELDS,$body);
    }
    if(count($header)>0){
    curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
    }
    //设置上传文件相关
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
    curl_setopt($ch,CURLOPT_MAXREDIRS,3);//递归
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
    switch ($type) {
    case 'GET':
    curl_setopt($ch,CURLOPT_HTTPGET,1);
    break;
    case 'POST':
    curl_setopt($ch,CURLOPT_POST,1);
    break;
    case 'PUT':
    curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'PUT');
    break;
    case 'DELETE':
    curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'DELETE');
    break;
    }
    curl_setopt($ch,CURLOPT_ENCODING,'gzip');
    curl_setopt($ch,CURLOPT_USERAGENT,'SSTS Browser/1.0');
    curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)');
    if(curl_errno($ch)){
    return curl_error($ch);
    }
    $content = curl_exec($ch);
    curl_close($ch);
    $result = json_decode($content,true);
    if(!empty($result)){
    return $result;
    }else{
    return $content;
    }
    }