码迷,mamicode.com
首页 > Web开发 > 详细

PHP Curl 函数,支持POST GET

时间:2014-08-21 09:48:13      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:curl   php   

/**
 * @author       default7<default7@zbphp.com>
 * @param        $url
 * @param string $method
 * @param array  $postData
 *
 * @return mixed|null|string
 */
function getHttpContent($url, $method = 'GET', $postData = array())
{
    $data = '';

    if (!empty($url)) {
        try {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, false);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_TIMEOUT, 30); //30秒超时
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            //curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
            if (strtoupper($method) == 'POST') {
                $curlPost = http_build_query($postData);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
            }
            $data = curl_exec($ch);
            curl_close($ch);
        } catch (Exception $e) {
            $data = null;
        }
    }

    return $data;
}

PHP Curl 函数,支持POST GET,布布扣,bubuko.com

PHP Curl 函数,支持POST GET

标签:curl   php   

原文地址:http://blog.csdn.net/default7/article/details/38725271

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!