curl模块(php版本)
幻冰小站 浏览次数:605 2020-08-20 10:23:01
/**
    *create:kuoqi
    *version:1.00
    *createTime:2019-08-05
    *remark:这个是一个独立的对象
*/
namespace    Kuoqi\Curl;
/**
    * Class Curl
    * @package Kuoqi
    */
class    Curl{
    //使用链接地址
    protected $Curl;
    //内部资源信息
    protected $Resource = NULL;
    //请求方式
    protected $Method = 'GET';
    //请求参数信息
    protected $Params = array();
    //封装请求头信息
    protected $HeadInfo = array('Content-Type:multipart/form-data');
    //使用用户信息
    protected $IsUser = true;
    //使用用户信息的用户名
    protected $UserName = '';
    //使用用户信息的密码
    protected $UserPass = '';
    //是否使用代理信息
    protected $IsProxy = true;
    //代理服务器地址
    protected $ProxyHost = '';
    //代理服务器端口
    protected $ProxyPort = '';
    //是否验证https
    protected $VerifyHost = false;
    protected $VerifyPeer = false;
    //是否支持重定向
    protected $useFollowLocal = false;
    //超时类型[S/MS]
    protected $TimeOutType = 'S';
    //超时时间
    protected $TimeOut = 6;
    //是否使用token
    protected $Cookie = '';
    //使用客户端信息
    protected $UserAgentList = array('Mozilla/5.0');
    //是否使用userAgent,如果使用直接列出[UserAgentLits]索引号即可,不使用则使用false
    protected $UserAgent = false;
    //返回头部信息
    public $ResponseHead = array();
    //返回信息
    public $Response = '';
    //返回信息基础信息
    public $ResponseText = '';
    //响应头类型
    public $ResponseType = '';
    //常规处理信息
    protected $Info = array();
    //发送请求
    public function send( $curl = '', $type = false, $data = array(), $ResopnseType = '')
    {
    $this->Curl = $curl;
    $this->Method = strtoupper($type);
    $this->Params = $data;
    $this->ResponseType = strtoupper($ResopnseType);
    $this->Resource = curl_init();
    $this->initRequest();
    $this->setInfo('curl',$this->Curl);
    $this->sendTo();
    $this->initResponse();
    return $this->Response;
    }
    final function sendTo(){
    curl_setopt($this->Resource,CURLOPT_URL,$this->Curl);
    curl_setopt($this->Resource,CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($this->Resource,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($this->Resource,CURLOPT_CUSTOMREQUEST,$this->Method);
    curl_setopt($this->Resource,CURLOPT_HTTPHEADER,$this->HeadInfo);
    curl_setopt($this->Resource,CURLOPT_SSL_VERIFYPEER,$this->VerifyHost);
    curl_setopt($this->Resource,CURLOPT_SSL_VERIFYHOST,$this->VerifyPeer);
    $this->ResponseText = curl_exec($this->Resource);
    $this->ResponseHead = curl_getinfo($this->Resource);
    curl_close($this->Resource);
    }
    
    final function useUseragent(){
    $agent = $this->UserAgentList[$this->UserAgent];
    $this->setInfo('userAgent',$agent);
    curl_setopt($this->Resource,CURLOPT_USERAGENT,$agent);
    }
    final function useUser(){
    $this->setInfo('useUser',true);
    curl_setopt($this->Resource,CURLOPT_USERPWD,$this->UserName.":".$this->UserPass);
    }
    final function useProxy(){
    $this->setInfo('useProxy',true);
    curl_setopt($this->Resource,CURLOPT_PROXYAUTH,CURLAUTH_BASIC);
    curl_setopt($this->Resource,CURLOPT_PROXY,$this->ProxyHost);
    curl_setopt($this->Resource,CURLOPT_PROXYPORT,$this->ProxyPort);
    }
    final function useCookie(){
    $this->setInfo('useToken',true);
    curl_setopt($this->Resource,CURLOPT_COOKIEJAR,$this->Cookie);
    curl_setopt($this->Resource,CURLOPT_COOKIEFILE,$this->Cookie);
    }
    final function useFollowLocation(){
    $this->setInfo('useFLocal',true);
    curl_setopt($this->Resource,CURLOPT_FOLLOWLOCATION,1);
    }
    final function useTimeout(){
    $this->setInfo('timeout',$this->TimeOut.$this->TimeOutType);
    if($this->TimeOutType == 'MS'){
    curl_setopt($this->Resource,CURLOPT_NOSIGNAL,true);
    curl_setopt($this->Resource,CURLOPT_TIMEOUT_MS,$this->TimeOut);
    }else{
    curl_setopt($this->Resource,CURLOPT_TIMEOUT,$this->TimeOut);
    }
    }
    protected function initRequest()
    {
    $this->UserAgent===false||$this->useUseragent();
    $this->IsUser&&$this->useUser();
    $this->IsProxy&&$this->useProxy();
    $this->Cookie&&$this->useCookie();
    $this->useFollowLocal&&$this->useFollowLocation();
    $this->useTimeout();
    $this->setInfo('method',$this->Method);
    switch($this->Method){
    case 'POST':
    self::initRequestPost();
    break;
    case 'form'://form表单请求数据处理
    self::requestDataJson();
    break;
    case 'json'://json数据要求请求处理
    self::$TYPE='POST';
    self::requestDataJson();
    break;
    case 'xml'://xml数据请求处理
    self::requestDataJson();
    break;
    case 'html'://html请求数据处理
    self::requestDataJson();
    break;
    case 'string'://字符串数据处理;
    self::requestDataJson();
    break;
    case 'file'://文件上传数据处理
    self::requestDataJson();
    break;
    default:$this->initRequestGet();
    }
    }
    //返回信息处理
    protected function initResponse(){
    $this->setInfo('responseType',$this->ResponseType);
    switch($this->ResponseType){
    case 'JSON':
    $this->Response = json_decode($this->ResponseText,true);break;
    default:$this->Response = $this->ResponseText;
    }
    }
    final function initRequestGet(){
    if(self::arr2param($this->Params)){
    $this->Curl = $this->Curl.'?'.self::arr2param($this->Params);
    }
    }
    final function initRequestPost(){
    curl_setopt($this->Resource,CURLOPT_POST,true);//启用post 传输
    curl_setopt($this->Resource,CURLOPT_POSTFIELDS,$this->Params);
    }
    final function setInfo($n,$v){
    $this->Info[$n] = $v;
    }
    public function toArray(){
    return $this->Info;
    }
    
    static protected function arr2param($input)
    {
    if(!is_array($input)){$input = array();}
    foreach($input as $k=>$v){
    if(isset($output)){
    $output .= '&'.$k.'='.$v;
    }else{
    $output = $k.'='.$v;
    }
    }
    if(!isset($output)){
    $output = false;
    }
    return $output;
    }
}