php之curl实现http请求(支持GET和POST)

php之curl实现http请求(支持GET和POST)

/**
 * curl实现http请求(支持GET和POST)
 * 作者:myth
 * function curl_request ($url[, $post=null, $timeout=10000, $config=array()])
 * @param $url      接口地址
 * @param $post     接口参数和值若为get方式则为空,默认为null
 * @param $timeout  超时时间,默认为10000
 * @param $config =     [
 *   //选填,头文件信息,默认看文档
 *   'httpheader'    =>    [
 *   //类型
 *   'Content-type: text/plain',
 *   //长度
 *   'Content-length: 100'
 *   ],
 *   //选填,用于伪装系统信息和浏览器信息
 *   'useragent'     =>    '',
 *   //选填,cookie信息
 *   'cookie'        =>    [],
 *   //是否输出数据流头文件信息
 *   'header'        =>    [],
 *   //是否进行编码
 *   'encoding'      =>    [
 *   //编码为
 *   'to'   =>   '',
 *   //原编码或者原编码的可能数组
 *   'from' =>   ''或者[]
 *   ],
 *   ]                配置信息默认为array()
 * @return array|bool|string
 */
function curl_request ($url, $post=null, $timeout=10000, $config=array()) {

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //返回原生的(Raw)输出
    curl_setopt($ch, CURLOPT_HEADER, false); //启用时会将头文件的信息作为数据流输出

    //设置https
    if (substr($url, 0, 5)=='https') {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//禁用后curl将终止从服务端进行验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    }

    //设置post内容
    if (!empty($post)) {
        if (!empty($config['encoding'])) {
            $post = mb_convert_encoding($post, $config['encoding']['from'], $config['encoding']['to']);
        }
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    }

    //设置超时时间
    if (!empty($timeout)) {
        curl_setopt($ch, CURLOPT_NOSIGNAL, true);
        curl_setopt($ch, CURLOPT_TIMEOUT_MS, $timeout);
    }

    //设置httpheader
    if (!empty($config['httpheader'])) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $config['httpheader']);
    }

    //设置useragent
    if (!empty($config['useragent'])) {
        curl_setopt($ch, CURLOPT_USERAGENT, $config['useragent']);
    }

    //携带cookie访问
    if (!empty($config['cookie'])) {
        curl_setopt($ch, CURLOPT_COOKIE, $config['cookie']);
    }

    // 设置代理服务器(测试用)
    //curl_setopt($ch,CURLOPT_PROXY,'127.0.0.1:8888');

    //
    if (!empty($config['header']) || (isset($config['cookie']) && !empty($config['cookie']))) {
        curl_setopt($ch, CURLOPT_HEADER, true); //启用时会将头文件的信息作为数据流输出
        $content = curl_exec($ch);
        list($header, $body) = explode("

", $content, 2); // 解析HTTP数据流
        preg_match("/set-cookie:([^
]*)/i", $header, $matches); // 解析COOKIE
        $cookie = $matches[1];
        $result = array('header'=>$header, 'cookie'=>$cookie, 'body'=>$body);
    } else {
        $result = curl_exec($ch);
    }

    curl_close($ch);

    //转换编码格式
    if (!empty($config['encoding'])) {
        if (is_array($result)) {
            foreach ($result as $k => $v) {
                $result[$k] = mb_convert_encoding($v, $config['encoding']['to'], $config['encoding']['from']);
            }
        } else {
            $result = mb_convert_encoding($result, $config['encoding']['to'], $config['encoding']['from']);
        }
    }

    return $result;
}
原文地址:https://www.cnblogs.com/littleelf/p/12287745.html