探索者

php curl post get put delete 方法

<?php
class HttpClient{

    public static $header=array();

    public static  function post($url,$ar=array(),$pic=false){
        return self::http($url,$ar,'POST',$pic);
    }

    public static function get($url,$ar=array()){
        return self::http($url,$ar);
    }

    public static function put($url,$ar=array()){
        return self::http($url,$ar,'PUT');
    }

    public static function delete($url,$ar=array()){
        return self::http($url,$ar,'DELETE');
    }

    /**
     * 发起一个HTTP/HTTPS的请求
     * @param $url 接口的URL
     * @param $params 接口参数   array('content'=>'test', 'name'=>'wangwu');
     * @param $method 请求类型    GET|POST
     * @param $multi 图片信息
     * @param $exts ssh pem ca 加密等信息
     * @return string
     */
    public static function http( $url , $params = array(), $method = 'GET' , $multi = false, $exts=array()){
        $ci = curl_init();
        curl_setopt($ci, CURLOPT_USERAGENT, 'PHP-SDK OAuth2.0');
        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 3);
        curl_setopt($ci, CURLOPT_TIMEOUT,$multi?30:5);
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);

        if($exts['cert']){
            curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, true);
            curl_setopt($ci, CURLOPT_SSLCERT, $exts['cert']['file']);
            curl_setopt($ci, CURLOPT_SSLCERTPASSWD, $exts['cert']['passwd']);
            curl_setopt($ci, CURLOPT_SSLCERTTYPE, $exts['cert']['type']);
        }else{
            curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
        }

        if($exts['ca']){
            curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, true);
            curl_setopt($ci, CURLOPT_CAINFO, $exts['ca']['file']);
        }else{
            curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);
        }

        curl_setopt($ci, CURLOPT_HEADER, false);

        switch ($method)
        {
            case 'PUT':
            case 'DELETE':
            case 'POST':
                if($method!='POST'){
                    curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method);
                }else{
                    curl_setopt($ci, CURLOPT_POST, TRUE);
                }
                if (!empty($params))
                {
                    if($multi)
                    {
                        foreach($multi as $key => $file)
                        {
                            $params[$key] = '@' . $file;
                        }
                        curl_setopt($ci, CURLOPT_POSTFIELDS, $params);
                        $headers[] = 'Expect: ';
                    }
                    else
                    {
                        curl_setopt($ci, CURLOPT_POSTFIELDS, is_array($params)?http_build_query($params):$params);
                    }
                }
                break;
            case 'GET':
                if (!empty($params))
                {
                    $url = $url . (strpos($url, '?') ? '&' : '?')
                        . (is_array($params) ? http_build_query($params) : $params);
                }
                break;
        }
        curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE );
        curl_setopt($ci, CURLOPT_URL, $url);
        if(self::$header){
            curl_setopt($ci, CURLOPT_HTTPHEADER, self::$header );
        }
        $response = curl_exec($ci);
        curl_close ($ci);
        return $response;
    }

}


15262

15262:香港赛马会官方网址www.smh0022.top为迎接祖国67周年庆,即日起注册即送38。更有电子游艺七部曲,到处是惊喜,邀您来战

2016-10-02 20:01:28回复

14652

14652: 

2015-06-30 17:19:54回复


京ICP备12040723号 关于易寻 联系我们 手机版