HTTP请求方式有好几种,平时常用的可能就是GET,POST。但是请求RESTful接口时,需要使用delete,put等

/**
* 发送HTTP请求方法
* @param  string $url    请求URL
* @param  array  $params 请求参数
* @param  string $method 请求方法GET/POST
* @return array  $data   响应数据
*/
function httpRequest ($url, $params, $method = 'GET', $header = array()){
    $opts = array(
            CURLOPT_TIMEOUT        => 30,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_HTTPHEADER     => $header
    );
    /* 根据请求类型设置特定参数 */
    switch(strtoupper($method)){
        case 'GET':
            $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
            break;
        case 'POST':
            $opts[CURLOPT_URL]     = $url;
            $opts[CURLOPT_POST] = 1;
            $opts[CURLOPT_POSTFIELDS] = $params;
            break;
        case 'PUT' : 
            $opts[CURLOPT_URL]     = $url;
            $opts[CURLOPT_CUSTOMREQUEST] = 'PUT';
            $opts[CURLOPT_POSTFIELDS]      = $params;
            break;
        case 'DELETE' :
            $opts[CURLOPT_URL]     = $url;
            $opts[CURLOPT_CUSTOMREQUEST] = 'DELETE';
            $opts[CURLOPT_POSTFIELDS]      = $params;
            break;
        default:
            throw new Exception('不支持的请求方式!');
    }
    /* 初始化并执行curl请求 */
    $ch = curl_init();
    curl_setopt_array($ch, $opts);
    $data  = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);
    if($error) throw new Exception('请求发生错误:' . $error);
    return  $data;
}