curl获取信息

function curlRequest($url, $isPost = false, $param = array(), $cookie = null, $header = null, $referer = "", $timeout = 60) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        if (is_array($header) && !empty($header)) { //设置header
            $set_head = array();
            foreach ($header as $k => $v) {
                $set_head[] = "$k:$v";
            }
            curl_setopt($ch, CURLOPT_HTTPHEADER, $set_head);
        } else {
            curl_setopt($ch, CURLOPT_HEADER, 0);
        }
        if ($isPost === true) {
            curl_setopt($ch, CURLOPT_POST, $isPost);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        }
        if (!empty($cookie)) {
            if (is_array($cookie)) {
                $cookies = implode(';', $cookie);
            } else {
                $cookies = $cookie;
            }
            curl_setopt($ch, CURLOPT_COOKIE, $cookies);
        }
        if (!empty($referer)) {
            curl_setopt($ch, CURLOPT_REFERER, $referer);
        }
        if( $timeout > 0 ) {
            curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $ret = curl_exec($ch);
        if (curl_errno($ch)) {
            $ret = '';
        }
        curl_close($ch);
        return $ret;
    }

 

posted @ 2015-06-25 12:15  黑面骑士  阅读(336)  评论(0编辑  收藏  举报