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

php自定义函数: 改进升级版curl

时间:2017-11-05 11:28:45      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:enc   turn   default   array   false   head   ini   class   函数   

<?php
function dcurl($url, $method = ‘GET‘, $postFields = null, $header = null) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt($ch, CURLOPT_FAILONERROR, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);

    if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https")
    {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    }

    switch ($method)
    {
        case ‘POST‘:
            curl_setopt($ch, CURLOPT_POST, true);
            if (!empty($postFields))
            {
                if (is_array($postFields) || is_object($postFields))
                {
                    if (is_object($postFields))
                        $postFields = Tools::object2array($postFields);
                    $postBodyString = "";
                    $postMultipart = false;
                    foreach ($postFields as $k => $v)
                    {
                        if ("@" != substr($v, 0, 1))
                        { //判断是不是文件上传
                            $postBodyString .= "$k=" . urlencode($v) . "&";
                        }
                        else
                        { //文件上传用multipart/form-data,否则用www-form-urlencoded
                            $postMultipart = true;
                        }
                    }
                    unset($k, $v);
                    if ($postMultipart)
                    {
                        curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
                    }
                    else
                    {
                        curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString, 0, -1));
                    }
                }
                else
                {
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
                }

            }
            break;
        default:
            if (!empty($postFields) && is_array($postFields))
                $url .= (strpos($url, ‘?‘) === false ? ‘?‘ : ‘&‘) . http_build_query($postFields);
            break;
    }
}
?>

  

 

 

 

 

 

;

php自定义函数: 改进升级版curl

标签:enc   turn   default   array   false   head   ini   class   函数   

原文地址:http://www.cnblogs.com/upup2015/p/7786801.html

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